简体   繁体   English

将Color作为#XXXXXX等字符串转换为System.Windows.Media.Brush的最简单方法

[英]Simpliest way to convert a Color as a string like #XXXXXX to System.Windows.Media.Brush

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-无论如何,我知道COlor转换不是那么直观,但我觉得我做得不对......这是好方法吗?

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 . 在这里查看XAML中的示例 You can pass any of those string formats in. 您可以传递任何这些字符串格式。

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)

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

只需使用ColorTranslator方法:

ColorTranslator.FromHtml("#010203");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将BitmapImage转换为System.Windows.Media.Brush - Convert BitmapImage to System.Windows.Media.Brush 将System.Windows.Media.Brush转换为System.Drawing.Brush - Convert System.Windows.Media.Brush to System.Drawing.Brush 将system.windows.media.brush转换为Hex颜色代码 - Converting system.windows.media.brush to Hex color code C#如何将system.windows.media.brush转换为system.drawing.brush - C# how to convert system.windows.media.brush to system.drawing.brush &#39;ColorAnimation&#39; 动画对象不能用于为属性 &#39;Background&#39; 设置动画,因为它是不兼容的类型 &#39;System.Windows.Media.Brush&#39; - 'ColorAnimation' animation object cannot be used to animate property 'Background' because it is of incompatible type 'System.Windows.Media.Brush' 试图更改文本的前景色并得到错误“无法将类型&#39;string&#39;隐式转换为&#39;windows.ui.xaml.Media.Brush&#39; - trying to change foreground color of text and getting error "can't implicitly convert type 'string' to 'windows.ui.xaml.Media.Brush' 无法将类型&#39;Windows.UI.Color&#39;隐式转换为&#39;Windows.UI.Xaml.Media.Brush&#39; - Cannot implicitly convert type 'Windows.UI.Color' to 'Windows.UI.Xaml.Media.Brush' 如何从 System.Drawing.Color 转换为 System.Windows.Media.Color? - How to convert from System.Drawing.Color to System.Windows.Media.Color? 从颜色转换为画笔 - convert from Color to brush System.Windows.Media.Color为颜色名称 - System.Windows.Media.Color to color name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM