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!!
|