I want to create a System.Drawing.Color
from a value like #FF00FF
or FF00FF
without needing to write code for that. There is any .NET built-in parser for that?
How to create a System.Drawing.Color from its hexadecimal RGB string?
98.5k Views Asked by Jader Dias At
6
There are 6 best solutions below
0

It is rather easy when you use the Convert-Class. The ToInt32
function has an overload with a second parameter which represents the base the string is in.
using System.Drawing
Color yourColor = Color.FromARGB(Convert.ToInt32("FF00FF", 16));
0

Use the ColorConverter class:
var converter = System.ComponentModel.TypeDescriptor.GetConverter( typeof( Color ) );
color = converter.ConvertFromString( "#FF00FF" );
This can also convert from the standard named colors e.g. ConvertFromString( "Blue" )
See here for a discussion of the standard .NET type conversion mechanisms.