devxlogo

Convert Hex to RGB in C#

Convert Hex to RGB in C#

The .NET System.Globalizaton’s NumberStyles has a parameter called AllowHexSpecifier that helps us to replace a hexString with RGB color.

public static Color HexToColor(string hexString){            //replace # occurences            if (hexString.IndexOf('#') != -1)                hexString = hexString.Replace("#", "");            int r,g,b = 0;             r = int.Parse(hexString.Substring(0, 2), NumberStyles.AllowHexSpecifier);             g = int.Parse(hexString.Substring(2, 2), NumberStyles.AllowHexSpecifier);             b = int.Parse(hexString.Substring(4, 2), NumberStyles.AllowHexSpecifier);            return Color.FromArgb(r, g, b);}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist