user2330678
user2330678

Reputation: 2311

Convert integer to color in WPF

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

Upvotes: 8

Views: 16243

Answers (3)

Mark Hall
Mark Hall

Reputation: 54562

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]));

Upvotes: 17

Grant Winney
Grant Winney

Reputation: 66501

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

Upvotes: 4

jmcilhinney
jmcilhinney

Reputation: 54477

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.

Upvotes: 1

Related Questions