How to convert integer to color in WPF? For example, I want to convert 16711935 to color.
How to do something like below in windows forms, in WPF?
myControl.Background = Color.FromArgb(myColorInt);
Use the BitConverter
Class to convert your value to a Byte Array, that way you do not need to import another namespace.
byte[] bytes = BitConverter.GetBytes(16711935);
this.Background = new SolidColorBrush( Color.FromArgb(bytes[3],bytes[2],bytes[1],bytes[0]));
You want to use System.Drawing.Color
, not System.Windows.Media.Color
:
var myColor = System.Drawing.Color.FromArgb(16711935);
Ooookay, not sure this is very pretty, but you could convert from one Color
class to the other, then use that in the SolidColorBrush
ctor:
myControl.Background = new SolidColorBrush(
System.Windows.Media.Color.FromArgb(myColor.A,myColor.R,myColor.G,myColor.B));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With