C# 将颜色作为字符串(如 #XXXXXX)转换为 System.Windows.Media.Brush 的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9178799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simpliest way to convert a Color as a string like #XXXXXX to System.Windows.Media.Brush
提问by Guillaume Slashy
I think that the title is clear !
我认为标题很清楚!
What I have now is :
我现在拥有的是:
System.Drawing.Color uiui = System.Drawing.ColorTranslator.FromHtml(myString);
var intColor = (uint)((uiui.A << 24) | (uiui.R << 16) | (uiui.G << 8) | (uiui.B << 0));
var bytes = BitConverter.GetBytes(uint.Parse(value));
var brush = new SolidColorBrush();
brush.Color = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);
1- myString is like #FFFFFF like I said in the title
1- myString 就像我在标题中所说的 #FFFFFF
2- This fails on the BitConverter.GetBytes line which surprises me cause I got the int representation on my Color !
2- 这在 BitConverter.GetBytes 行上失败了,这让我感到惊讶,因为我在我的 Color 上得到了 int 表示!
3- Anyway, I know that COlor conversion are not that intuitive but I feel like I'm not doing it right... Is that the good way ?
3-无论如何,我知道颜色转换不是那么直观,但我觉得我做得不对......这是好方法吗?
采纳答案by Ray
You can use the System.Windows.Media.ColorConverter
您可以使用 System.Windows.Media.ColorConverter
var color = (Color)ColorConverter.ConvertFromString("#FF010203");
//OR
var color = (Color)ColorConverter.ConvertFromString("#010203");
//OR
var color = (Color)ColorConverter.ConvertFromString("Red");
//and then:
var brush = new SolidColorBrush(color);
It's pretty flexible as to what it accepts. Have a look at the examples in XAML here. You can pass any of those string formats in.
它接受的内容非常灵活。在此处查看XAML中的示例。您可以传入任何这些字符串格式。
Note: These are all in System.Windows.Media(for WPF) not to be confused with System.Drawing(for WinForms)
注意:这些都在System.Windows.Media(对于 WPF)不要与System.Drawing(对于 WinForms)混淆
回答by Anthony Shaw
This is the helper class that I've used in the past
这是我过去用过的辅助类
public static Color HexStringToColor(string hexColor)
{
string hc = ExtractHexDigits(hexColor);
if (hc.Length != 6)
{
// you can choose whether to throw an exception
//throw new ArgumentException("hexColor is not exactly 6 digits.");
return Color.Empty;
}
string r = hc.Substring(0, 2);
string g = hc.Substring(2, 2);
string b = hc.Substring(4, 2);
Color color = Color.Empty;
try
{
int ri = Int32.Parse(r, NumberStyles.HexNumber);
int gi = Int32.Parse(g, NumberStyles.HexNumber);
int bi = Int32.Parse(b, NumberStyles.HexNumber);
color = Color.FromArgb(ri, gi, bi);
}
catch
{
// you can choose whether to throw an exception
//throw new ArgumentException("Conversion failed.");
return Color.Empty;
}
return color;
}
and an additional helper class
和一个额外的助手类
public static string ExtractHexDigits(string input)
{
// remove any characters that are not digits (like #)
var isHexDigit
= new Regex("[abcdefABCDEF\d]+", RegexOptions.Compiled);
string newnum = "";
foreach (char c in input)
{
if (isHexDigit.IsMatch(c.ToString()))
{
newnum += c.ToString();
}
}
return newnum;
}
回答by DiSaSteR
Just use ColorTranslator method:
只需使用 ColorTranslator 方法:
ColorTranslator.FromHtml("#010203");

