How to convert Hexadecimal #FFFFFF to System.Drawing.Color

C#asp.net

C# Problem Overview


> Possible Duplicate:
> How to get Color from Hex color code using .NET?

I want to convert a string like #FFFFFF to System.Drawing.Color. How do you do that?

C# Solutions


Solution 1 - C#

string hex = "#FFFFFF";
Color _color = System.Drawing.ColorTranslator.FromHtml(hex);

Note: the hash is important!

Solution 2 - C#

You can do

var color =  System.Drawing.ColorTranslator.FromHtml("#FFFFFF");

Or this (you will need the System.Windows.Media namespace)

var color = (Color)ColorConverter.ConvertFromString("#FFFFFF");

Solution 3 - C#

Remove the '#' and do

Color c = Color.FromArgb(int.Parse("#FFFFFF".Replace("#",""),
                         System.Globalization.NumberStyles.AllowHexSpecifier));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1531040View Question on Stackoverflow
Solution 1 - C#vargView Answer on Stackoverflow
Solution 2 - C#SidAhmedView Answer on Stackoverflow
Solution 3 - C#codeteqView Answer on Stackoverflow