views:

506

answers:

4

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?

+4  A: 

You mean like this:

using System.Convert
using System.Drawing

Color yourColor = Color.FromARGB(ToInt32("FF00FF", 16));
Bobby
+4  A: 

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.

Phil Devaney
+7  A: 
ColorTranslator.FromHtml("#FF00FF");
João Angelo
+2  A: 

You can use the System.Drawing.ColorTranslator static method FromHtml.

use:

System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
Pat