Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



C# .NET: Convert System.Drawing.Color to HTML color

Here is a small example of how to convert a System.Drawing.Color to the HTML color format (Hex value or HTML color name value) and back.

System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#F5F7F8");
String strHtmlColor = System.Drawing.ColorTranslator.ToHtml(c);

 

Currently rated 4.4 by 17 people

  • Currently 4.35294/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: General
Posted by crpietschmann on Thursday, June 29, 2006 2:48 PM
Permalink | Comments (5) | Post RSSRSS comment feed

Related posts

Comments

ניו יורק

Sunday, July 30, 2006 2:58 AM

ניו יורק

System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#F5F7F8");
String strHtmlColor = System.Drawing.ColorTranslator.ToHtml

thanks for the tip

Shriaknth

Tuesday, August 22, 2006 5:41 PM

Shriaknth

This piece of code is very good. Thanks it was very helpful.

Paul Diston

Saturday, November 04, 2006 5:23 PM

Paul Diston

If you wish to use the above code on the Compact Framework you can using the OpenNETCF.Drawing assembly which has an implementation of the color translator. Thank you for your help.

D

Tuesday, December 05, 2006 9:03 PM

D

Aahh, eventually...works like a bomb

DW

Sunday, February 25, 2007 1:01 AM

DW

In case you wanted to get the Hex value (#FF12200) of a color and not the html name of the color (ie. Color.Black returns "Black"), then you'll need to do the following.

From the AGRB value of the Color
dim s as String = Hex(Color.ToArgb)
'This gives you a string with the hex values of the Alpha, Red, Green, and Blue. You can strip off the first two characters of the string to get just the RGB
s = s.Substring(2)

'Or you can get each of the hex values for the red, green, and blue

s = Hex(Color.R) & Hex(Color.G) & Hex(Color.B)

'And of course, if you need the pound sign, you can add it to the front of the string
s = "#" & s

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann