Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi
i want to convert from System.Drawing.Color to HTML color, i want the color code not the color name

C#
MessageBox.Show(System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.Red));


output: Red
i want: #FF0000

thanx
Posted
Updated 19-Feb-21 5:00am

Try:
C#
private static String ConverteToHex(System.Drawing.Color c)
{
    return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
 
Share this answer
 
v2
Here is a snippet to change the color Red to the HTML output

C#
using System.Drawing;

Color colorRed = ColorTranslator.FromHtml("Red");
Console.WriteLine("colorRed: " + colorRed);
string htmlHexColorValueThree = String.Format("#{0:X2}{1:X2}{2:X2}", colorRed.R, colorRed.G, colorRed.B);
Console.WriteLine("htmlHexColorValueThree: " + htmlHexColorValueThree);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900