C#: Convert Drawing.Color to HTML Hex Color Value and Back

I was working on a small project that needed to colorize text from stored values and bitmap gradients which involved getting color values from individual pixels as RGB values or System.Drawing.Color values. The following code demonstrates how to convert back-and-forth between System.Drawing.Color and string values (#XXXXXX or the name). I’ve seen how some people have gone out of their way to do these conversions with their own code implementations, but why recreate what’s already in the .NET Framework?

I must admit though, I didn’t think this was already built-in until I searched MSDN.

 1 using System.Drawing;
2 using System;
3
4 public partial class ColorConvert : System.Web.UI.Page
5 {
6 protected void Page_Load(object sender, EventArgs e)
7 {
8 //convert to the HTML color value of a
9 //known System.Drawing.Color
10 string htmlNamedColorValue =
11 ColorTranslator.ToHtml(Color.Crimson);
12 //output: "Crimson"
13
14 //convert to the HTML hex color value from
15 //System.Drawing.Color with RGB values (208,0,0)
16 string htmlHexColorValueTwo =
17 ColorTranslator.ToHtml(Color.FromArgb(0, 208, 0, 0));
18 //output: "#D00000"
19
20 //convert to System.Drawing.Color from HTML hex color value
21 Color colorValueFrmHex =
22 ColorTranslator.FromHtml("#FFFF33");
23 //output: System.Drawing.Color with RGB values (255,25,51)
24
25 //convert to System.Drawing.Color from HTML known color
26 Color colorValue =
27 ColorTranslator.FromHtml("DarkRed");
28 //output: Color.DarkRed
29
30 string htmlHexColorValueThree =
31 String.Format("#{0:X2}{1:X2}{2:X2}", colorValue.R,
32 colorValue.G,
33 colorValue.B);
34 //output: "#8B0000"
35 }
36 }

kick it on DotNetKicks.com


Tags: , , , , , , ,

Bookmark and Share

Comments

Mike Johnston
Jun 21, 2008 7:01 AM
Mike Johnston Hey thanks for the tip!

Mike
Carlo
Jun 21, 2008 1:58 PM
Carlo You're welcome. Thanks for stopping by.
CSS
Jun 29, 2008 8:54 PM
CSS Does it mean that every system color has corresponding HTML color. I mean we have only a few web safe colors.
Carlo
Jun 29, 2008 9:48 PM
Carlo I don't know if I understood your question correctly, but I think any color can be expressed as its hex value (#XXXXXX) if it doesn't have a named color, like 'Blue', 'Red', etc.
Gabriel Rodriguez
Sep 5, 2008 8:10 AM
Gabriel Rodriguez It's weird.

I tried calling a C# method that accepts a parameter of type Color, and tried calling the method with something like

ChangeBackground(ColorTranslator.FromHtml("#FFCC00"))

or whatever HTML HEX Code, and it would never work. I had to call the method with a specific Color type created from a Known Color name.

Does anyone know why is it that the ColorTranslator.FromHtml is not working for me? Thanks
Valdir Silva
Nov 15, 2008 10:39 AM
Valdir Silva Incredible!
Excellent!
Very good!
Among many other comments that can be attributed to this post.

Congratulations .... great post ...

Valdir Silva
Brasil!!!
(Brazilian)
Prathiba
Jun 18, 2009 4:39 AM
Prathiba hey thanks a ton..

Excellent!! tat's exactly wat i was lookin for..

Want to leave a comment?