在 WPF 中将整数转换为颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21247974/
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
Convert integer to color in WPF
提问by user2330678
How to convert integer to color in WPF? For example, I want to convert 16711935 to color.
如何在 WPF 中将整数转换为颜色?例如,我想将 16711935 转换为颜色。
How to do something like below in windows forms, in WPF?
如何在 Windows 窗体中,在 WPF 中执行以下操作?
myControl.Background = Color.FromArgb(myColorInt);
回答by Mark Hall
Use the BitConverterClass to convert your value to a Byte Array, that way you do not need to import another namespace.
使用BitConverterClass 将您的值转换为字节数组,这样您就不需要导入另一个命名空间。
byte[] bytes = BitConverter.GetBytes(16711935);
this.Background = new SolidColorBrush( Color.FromArgb(bytes[3],bytes[2],bytes[1],bytes[0]));
回答by vivat pisces
You want to use System.Drawing.Color, not System.Windows.Media.Color:
你想使用System.Drawing.Color,而不是System.Windows.Media.Color:
var myColor = System.Drawing.Color.FromArgb(16711935);
Ooookay, not sure this is very pretty, but you could convert from one Colorclass to the other, then use that in the SolidColorBrushctor:
Ooookay,不确定这是否非常漂亮,但是您可以从一个Color类转换为另一个类,然后在SolidColorBrushctor 中使用它:
myControl.Background = new SolidColorBrush(
System.Windows.Media.Color.FromArgb(myColor.A,myColor.R,myColor.G,myColor.B));
回答by jmcilhinney
The System.Windows.Media.Color structure has similar methods but they have parameters of type Byte. You can use the BitConverter class to convert between an array of Bytes and an Int32.
System.Windows.Media.Color 结构具有类似的方法,但它们具有 Byte 类型的参数。您可以使用 BitConverter 类在字节数组和 Int32 之间进行转换。

