Get brush from hex color in WPF, Silverlight, Windows Phone

For an app I created I needed to create a solidcolorbrush from hex RGB string. Seems a simple task, but there is no simple way of doing this. But I’ve created simple method that does just this:

public static SolidColorBrush GetBrushFromHexString(string aarrggbb)
{
    string xamlString = "<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Background=\"" + aarrggbb + "\"/>";
    Canvas c = (Canvas)System.Windows.Markup.XamlReader.Load(xamlString);
    return (SolidColorBrush)c.Background;
}

It can be used in WPF, Silverlight and Windows Phone.

Comments are closed.