Start Debugging
2012-01-21 Updated 2023-11-05 csharp

C# Convert Hex To Color

A C# extension method that converts hex color codes (both RGB and ARGB formats) to Color objects.

Below you have an extension method that can help you convert any hex color code to a Color object.

public static Color ToColor(this string hexColor)
{
   return Color.FromArgb(
      Convert.ToByte(hexColor.ToString().Substring(1, 2), 16),
      Convert.ToByte(hexColor.ToString().Substring(3, 2), 16),
      Convert.ToByte(hexColor.ToString().Substring(5, 2), 16),
      Convert.ToByte(hexColor.ToString().Substring(7, 2), 16));
}

The method above can only convert 8-character ARGB color codes. In order to convert simple RGB codes too, we will check the length of the string (9 = ARGB and 7 = RGB) and in case it’s 7, we will add the alpha to our string as FF (255 — opaque) and only then return the color.

public static Color ToColor(this string hexColor)
{
   string tempHexColor = string.Empty;
   if (hexColor.Length == 7)
      tempHexColor = "#FF" + hexColor.Substring(1,6);
   return Color.FromArgb(
      Convert.ToByte(tempHexColor.Substring(1, 2), 16),
      Convert.ToByte(tempHexColor.Substring(3, 2), 16),
      Convert.ToByte(tempHexColor.Substring(5, 2), 16),
      Convert.ToByte(tempHexColor.Substring(7, 2), 16));
}

We should also do something in case the hex color code is not actually a hex color code. So add another if statement after the one we added already and check again for the length of the string; if it’s not equal to 9 then it’s not good so feel free to return any color you want (I will return transparent). The final method looks like this:

public static Color ToColor(this string hexColor)
{
   string tempHexColor = string.Empty;
   if (hexColor.Length == 7)
      tempHexColor = "#FF" + hexColor.Substring(1,6);
   else
      tempHexColor = hexColor;
   if (tempHexColor.Length != 9)
      tempHexColor = "#00000000";
   return Color.FromArgb(
      Convert.ToByte(tempHexColor.Substring(1, 2), 16),
      Convert.ToByte(tempHexColor.Substring(3, 2), 16),
      Convert.ToByte(tempHexColor.Substring(5, 2), 16),
      Convert.ToByte(tempHexColor.Substring(7, 2), 16));
}

How to use it:

string myHexString = "#78196DFD";
Color myColor = new Color();
myColor = myHexString.ToColor();
< Back