ColorTranslator.ToHtml () error
I need a hex string of color, so I am using a property ColorTranslator.ToHtml()
that returns a hex string of color.
If I choose a random color it returns " # FFF0B6 " etc. However, if I choose a system color, for example Color.Black
, it returns " Black " in the string.
I need hex color codes in a string, whether they are defined in the system or not. Any suggestions?
+3
source to share
1 answer
I found this extension method worked well for me:
public static string ToHexValue(this Color color)
{
return "#" + color.R.ToString("X2") +
color.G.ToString("X2") +
color.B.ToString("X2");
}
According to MSDN , this is really the right thing to do ColorTranslator.ToHtml()
.
+8
source to share