Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer to color in WPF

Tags:

c#

.net

colors

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);
like image 766
user2330678 Avatar asked Jan 21 '14 02:01

user2330678


3 Answers

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]));
like image 134
Mark Hall Avatar answered Oct 19 '22 07:10

Mark Hall


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));
like image 43
Grant Winney Avatar answered Oct 19 '22 06:10

Grant Winney


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.

like image 1
jmcilhinney Avatar answered Oct 19 '22 07:10

jmcilhinney