c# - How do I get the color from a hexadecimal color code using .NET?

ID : 2907

viewed : 178

Tags : c#wpfcolorshexc#

Top 5 Answer for c# - How do I get the color from a hexadecimal color code using .NET?

vote vote


I'm assuming that's an ARGB code... Are you referring to System.Drawing.Color or System.Windows.Media.Color? The latter is used in WPF for example. I haven't seen anyone mention it yet, so just in case you were looking for it:

using System.Windows.Media;  Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991"); 
vote vote


Assuming you mean the HTML type RGB codes (called Hex codes, such as #FFCC66), use the ColorTranslator class:

System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#FFCC66"); 

If, however you are using an ARGB hex code, you can use the ColorConverter class from the System.Windows.Media namespace:

Color col = ColorConverter.ConvertFromString("#FFDFD991") as Color; //or      = (Color) ColorConverter.ConvertFromString("#FFCC66") ; 
vote vote


If you don't want to use the ColorTranslator, you can do it in easily:

string colorcode = "#FFFFFF00"; int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber); Color clr = Color.FromArgb(argb); 

The colorcode is just the hexadecimal representation of the ARGB value.


If you need to use 4 values instead of a single integer, you can use this (combining several comments):

string colorcode = "#FFFFFF00";     colorcode = colorcode.TrimStart('#');  Color col; // from System.Drawing or System.Windows.Media if (colorcode.Length == 6)     col = Color.FromArgb(255, // hardcoded opaque                 int.Parse(colorcode.Substring(0,2), NumberStyles.HexNumber),                 int.Parse(colorcode.Substring(2,2), NumberStyles.HexNumber),                 int.Parse(colorcode.Substring(4,2), NumberStyles.HexNumber)); else // assuming length of 8     col = Color.FromArgb(                 int.Parse(colorcode.Substring(0, 2), NumberStyles.HexNumber),                 int.Parse(colorcode.Substring(2, 2), NumberStyles.HexNumber),                 int.Parse(colorcode.Substring(4, 2), NumberStyles.HexNumber),                 int.Parse(colorcode.Substring(6, 2), NumberStyles.HexNumber)); 

Note 1: NumberStyles is in System.Globalization.
Note 2: please provide your own error checking (colorcode should be a hexadecimal value of either 6 or 8 characters)

vote vote


The three variants below give exactly the same color. The last one has the benefit of being highlighted in the Visual Studio 2010 IDE (maybe it's ReSharper that's doing it) with proper color.

var cc1 = System.Drawing.ColorTranslator.FromHtml("#479DEE");  var cc2 = System.Drawing.Color.FromArgb(0x479DEE);  var cc3 = System.Drawing.Color.FromArgb(0x47, 0x9D, 0xEE); 
vote vote


There is also this neat little extension method:

static class ExtensionMethods {     public static Color ToColor(this uint argb)     {         return Color.FromArgb((byte)((argb & -16777216)>> 0x18),                                     (byte)((argb & 0xff0000)>> 0x10),                                  (byte)((argb & 0xff00) >> 8),                               (byte)(argb & 0xff));     } } 

In use:

Color color = 0xFFDFD991.ToColor(); 

Top 3 video Explaining c# - How do I get the color from a hexadecimal color code using .NET?