CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to convert string color code or HTML color code to System.Drawing.Color and vice versa in ASP.Net?
Submitted By Satheesh Babu B
On 6/29/2010 7:12:55 AM
Tags: ASP.Net,CodeDigest  

How to convert string color code or HTML code to System.Drawing.Color and vice versa in ASP.NET?

 

This is a small tip which will help you to convert a string color code(#F82080) or color name(Red) to System.Drawing.Color object and a System.Drawing.Color object to string color code.


For example, ASP.Net controls like textbox control will accept only System.Drawing.Color for its styling options like Background color.

 

1. Converting string color code to System.Drawing.Color object,

 

txtUserID.BackColor = System.Drawing.ColorTranslator.FromHtml("Yellow");

 

The above code can also be written as,

 

txtUserID.BackColor = System.Drawing.ColorTranslator.FromHtml("#F37492");

 

2. Converting System.Drawing.Color object to string color code

string hexcolor = System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.Red);

 

Happy Coding!!

 

 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..