wpf 从十六进制颜色值创建 SolidColorBrush

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10062376/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:21:59  来源:igfitidea点击:

Creating SolidColorBrush from hex color value

wpf

提问by Mahesha999

I want to create SolidColorBrush from Hex value such as #ffaacc. How can I do this?

我想从十六进制值(例如#ffacc)创建 SolidColorBrush。我怎样才能做到这一点?

On MSDN, I got :

在 MSDN 上,我得到了:

SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);

So I wrote (considering my method receives color as #ffaacc):

所以我写了(考虑到我的方法接收颜色为#ffaacc):

Color.FromRgb(
  Convert.ToInt32(color.Substring(1, 2), 16), 
  Convert.ToInt32(color.Substring(3, 2), 16), 
  Convert.ToInt32(color.Substring(5, 2), 16));

But this gave error as

但这给出了错误

The best overloaded method match for 'System.Windows.Media.Color.FromRgb(byte, byte, byte)' has some invalid arguments

The best overloaded method match for 'System.Windows.Media.Color.FromRgb(byte, byte, byte)' has some invalid arguments

Also 3 errors as: Cannot convert int to byte.

还有 3 个错误: Cannot convert int to byte.

But then how MSDN example works?

但是 MSDN 示例是如何工作的呢?

回答by Chris Ray

Try this instead:

试试这个:

(SolidColorBrush)(new BrushConverter().ConvertFrom("#ffaacc"));

回答by GJHix

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

如何使用 .NET 从十六进制颜色代码中获取颜色?

This I think is what you are after, hope it answers your question.

我认为这就是您所追求的,希望它能回答您的问题。

To get your code to work use Convert.ToByte instead of Convert.ToInt...

要让您的代码正常工作,请使用 Convert.ToByte 而不是 Convert.ToInt...

string colour = "#ffaacc";

Color.FromRgb(
Convert.ToByte(colour.Substring(1,2),16),
Convert.ToByte(colour.Substring(3,2),16),
Convert.ToByte(colour.Substring(5,2),16));

回答by Jon Vielhaber

I've been using:

我一直在使用:

new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffaacc"));

回答by Mahesha999

using System.Windows.Media;

byte R = Convert.ToByte(color.Substring(1, 2), 16);
byte G = Convert.ToByte(color.Substring(3, 2), 16);
byte B = Convert.ToByte(color.Substring(5, 2), 16);
SolidColorBrush scb = new SolidColorBrush(Color.FromRgb(R, G, B));
//applying the brush to the background of the existing Button btn:
btn.Background = scb;

回答by Neil B

If you don't want to deal with the pain of the conversion every time simply create an extension method.

如果您不想每次都处理转换的痛苦,只需创建一个扩展方法。

public static class Extensions
{
    public static SolidColorBrush ToBrush(this string HexColorString)
    {
        return (SolidColorBrush)(new BrushConverter().ConvertFrom(HexColorString));
    }    
}

Then use like this: BackColor = "#FFADD8E6".ToBrush()

然后像这样使用: BackColor = "#FFADD8E6".ToBrush()

Alternately if you could provide a method to do the same thing.

或者,如果您可以提供一种方法来做同样的事情。

public SolidColorBrush BrushFromHex(string hexColorString)
{
    return (SolidColorBrush)(new BrushConverter().ConvertFrom(hexColorString));
}

BackColor = BrushFromHex("#FFADD8E6");

回答by Dr. Somebody

vb.net version

vb.net 版本

Me.Background = CType(New BrushConverter().ConvertFrom("#ffaacc"), SolidColorBrush)