Convert hex string to color

c#, colors, hex

Solution

Try this:

string sColor = Request.QueryString["color"]; // sColor is now #AA4643
Int32 iColorInt = Convert.ToInt32(sColor.Substring(1),16); 
Color curveColor = System.Drawing.Color.FromArgb(iColorInt); 

Problem

I am passing a hex value in my QueryString. I'd like to convert that to a color to use the ForeColor in a cell in a grid view. Tried both `System.Drawing.ColorTranslator.FromHtml()` and `System.Drawing.Color.FromArgb()` with no luck. My QueryString is urlencoded so the part that matters looks like: ``` QueryString...&color=%23AA4643 ``` Below is how I have tried .FromArg: ``` string sColor = Request.QueryString["color"]; // sColor is now #AA4643 Int32 iColorInt = Convert.ToInt32(sColor,16); //Get error message - Could not find any recognizable digits Color curveColor = System.Drawing.Color.FromArgb(iColorInt); //Never makes it here ``` And here is how I have tried .FromHtml: ``` string sColor = Request.QueryString["color"]; System.Drawing.Color myColor = new System.Drawing.Color(); myColor = System.Drawing.ColorTranslator.FromHtml(sColor); ``` In this case myColor gets set to - myColor = "{Name=ffaa4643, ARGB=(255, 170, 70, 67)}" But when I go to use it I get an error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Any and all help greatly appreciated

Original source