ASP.NET/C#: Convert HTML Colors to System.Drawing.Color

Posted on Wednesday, May 14th, 2008. Filed under ASP.NET/C#.

HTML colors come in all sorts of flavors: hexadecimal (#0066ff), named colors (e.g. LightSkyBlue…here’s the link).

Many people may be very comfortable with working with HTML colors, so if you’re in C# and needing to get the .Net equivalent, you can translate directly from HTML colors with System.Drawing.ColorTranslator.FromHtml(). Here’s the usage:

System.Drawing.Color myColor = System.Drawing.ColorTranslator.FromHtml("Red");
or
System.Drawing.ColorTranslator.FromHtml("#ff0000");

Controls require the System.Drawing.Color type:

TextBox t = new TextBox();
t.BackColor = System.Drawing.Color.Red;

Also useful is System.Drawing.ColorConverter. Here’s the MSDN link for that. But working from the HTML colors may be more familiar to you.

By Byron Bennett
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Slashdot
  • SphereIt
  • StumbleUpon
  • Technorati
  • YahooMyWeb
  • Google
  • Live
  • Propeller
Sphere: Related Content



You can now:

Leave A Comment (0). Or, Leave A Trackback. Or, view Comments Feed.

Related Reading:


Subscribe without commenting


Leave a Reply

Note: Any comments are permitted only because the site owner is letting you post, and any comments will be removed for any reason at the absolute discretion of the site owner.