[c#] How to convert Hexadecimal #FFFFFF to System.Drawing.Color

Possible Duplicate:
How to get Color from Hex color code using .NET?

I want to convert a string like #FFFFFF to System.Drawing.Color. How do you do that?

This question is related to c# asp.net

The answer is


string hex = "#FFFFFF";
Color _color = System.Drawing.ColorTranslator.FromHtml(hex);

Note: the hash is important!


Remove the '#' and do

Color c = Color.FromArgb(int.Parse("#FFFFFF".Replace("#",""),
                         System.Globalization.NumberStyles.AllowHexSpecifier));

You can do

var color =  System.Drawing.ColorTranslator.FromHtml("#FFFFFF");

Or this (you will need the System.Windows.Media namespace)

var color = (Color)ColorConverter.ConvertFromString("#FFFFFF");