在C#中将VBColor值转换为HEX值

时间:2015-07-08 00:57:44

标签: c# colors hex

有没有办法在C#中将vbcolor值转换为十六进制值?例如:& H004080(vbcolor)到#804000(十六进制值)。或者我应该在外部转换值,然后将转换后的值放入我的代码中?

2 个答案:

答案 0 :(得分:1)

以下是我的工作方式,迄今为止有效:

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;
}

答案 1 :(得分:0)

您需要将十六进制颜色转换为字符串:

来自此处的Vb代码段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# - 或者使用此问题中描述的这个库:

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

using System.Windows.Media;

Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");