How to convert Color to Hex and Hex to Color using C# and VB.NET

To convert a Color to Hex and vice versa using C# and VB.NET you can use the snippets below.

Sample C#

private static String ColorToHex(Color inputColor)
{
	return ColorTranslator.ToHtml(inputColor);
}

private static Color HexToColor(string hexInput)
{
	return ColorTranslator.FromHtml(hexInput);
}

Sample VB.NET (autoconverted)

Private Shared Function ColorToHex(inputColor As Color) As [String]
	Return ColorTranslator.ToHtml(inputColor)
End Function

Private Shared Function HexToColor(hexInput As String) As Color
	Return ColorTranslator.FromHtml(hexInput)
End Function

Please Share us:
Please rate this snippet

One thought on “How to convert Color to Hex and Hex to Color using C# and VB.NET

Leave a Reply