正文
wpf中将string格式的颜色转换成color类型
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
wpf中Brushes有很多对应的颜色,先盗张图,每个颜色对于的名称和ARGB值有了,问题是有时候我们取到的颜色是ARGB值,而且是string类型的,该怎么转换成color呢,只有转换成color之后才能转换成brush。
这个问题的解决代码如下:
private Color StringToColor(string colorStr)//传入string,得到Color
{
Byte[] argb = new Byte[];
for (int i = ; i < ; i++)
{
char[] charArray= colorStr.Substring(i * + , ).ToCharArray();
//string str = "11";
Byte b1 = toByte(charArray[]);
Byte b2 = toByte(charArray[]);
argb[i] = (Byte)(b2 | (b1 << ));
}
return Color.FromArgb(argb[], argb[], argb[], argb[]);//#FFFFFFFF
}
private static byte toByte(char c)
{
byte b = (byte)"0123456789ABCDEF".IndexOf(c);
return b;
}
传入第一张图中的任意一个颜色的ARGB值,都能得到对于的color。之后需要转换成Brush的话只需要使用一句代码:
Brush color2 = new SolidColorBrush(color);