0

Is there any way to convert vbcolor value to hex value in C#? For example: &H004080 (vbcolor) to #804000 (hex value). Or should I convert the value externally and then put the converted value into my code?

YWah
  • 571
  • 13
  • 38

2 Answers2

1

Here's how I done and it worked so far:

public static string VBColorToHexConverter(string vbColor) 
{
  string hexValue;
  string r = "", g = "", b = "";

  char[] vbValue = vbColor.ToCharArray();

  for (int i = 0; i < vbValue.Length; i++) 
  {
    r = vbValue[6].ToString() + vbValue[7].ToString();
    g = vbValue[4].ToString() + vbValue[5].ToString();
    b = vbValue[2].ToString() + vbValue[3].ToString();
  }

  hexValue = "#" + r + g + b;

  return hexValue;
}
YWah
  • 571
  • 13
  • 38
0

You need to perform a conversion of hex color to string:

Vb code snippet from here Convert hex color string to RGB color:

Public Function ConvertToRbg(ByVal HexColor As String) As Color
    Dim Red As String
    Dim Green As String
    Dim Blue As String
    HexColor = Replace(HexColor, "#", "")
    Red = Val("&H" & Mid(HexColor, 1, 2))
    Green = Val("&H" & Mid(HexColor, 3, 2))
    Blue = Val("&H" & Mid(HexColor, 5, 2))
    Return Color.FromArgb(Red, Green, Blue)
End Function

C# - Or use this library described on this question:

How to get Color from Hexadecimal color code using .NET?

using System.Windows.Media;

Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");
Community
  • 1
  • 1
Thiago Avelino
  • 818
  • 6
  • 7
  • Hi @Thiago, I;ve tried with the ColorConverter in C#, but it doesn't work since my input value is on vb color format. I've to convert the vb color value to hex value first, then I put the converted value to the ColorConverter and it works. My problem now is the conversion from vb value to hex value. – YWah Jul 08 '15 at 01:31
  • YWah, I searched for that and I believe your answer is the best you can have on the conversion that you need. – Thiago Avelino Jul 09 '15 at 01:03