Creating SolidColorBrush from hex color value

Wpf

Wpf Problem Overview


I want to create SolidColorBrush from Hex value such as #ffaacc. How can I do this?

On MSDN, I got :

SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);

So I wrote (considering my method receives color as #ffaacc):

Color.FromRgb(
  Convert.ToInt32(color.Substring(1, 2), 16), 
  Convert.ToInt32(color.Substring(3, 2), 16), 
  Convert.ToInt32(color.Substring(5, 2), 16));

But this gave error as

The best overloaded method match for 'System.Windows.Media.Color.FromRgb(byte, byte, byte)' has some invalid arguments

Also 3 errors as: Cannot convert int to byte.

But then how MSDN example works?

Wpf Solutions


Solution 1 - Wpf

Try this instead:

(SolidColorBrush)new BrushConverter().ConvertFrom("#ffaacc");

Solution 2 - Wpf

I've been using:

new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffaacc"));

Solution 3 - Wpf

https://stackoverflow.com/questions/2109756/how-to-get-color-from-hex-color-code-using-net

This I think is what you are after, hope it answers your question.

To get your code to work use Convert.ToByte instead of Convert.ToInt...

string colour = "#ffaacc";

Color.FromRgb(
Convert.ToByte(colour.Substring(1,2),16),
Convert.ToByte(colour.Substring(3,2),16),
Convert.ToByte(colour.Substring(5,2),16));

Solution 4 - Wpf

using System.Windows.Media;

byte R = Convert.ToByte(color.Substring(1, 2), 16);
byte G = Convert.ToByte(color.Substring(3, 2), 16);
byte B = Convert.ToByte(color.Substring(5, 2), 16);
SolidColorBrush scb = new SolidColorBrush(Color.FromRgb(R, G, B));
//applying the brush to the background of the existing Button btn:
btn.Background = scb;

Solution 5 - Wpf

If you don't want to deal with the pain of the conversion every time simply create an extension method.

public static class Extensions
{
    public static SolidColorBrush ToBrush(this string HexColorString)
    {
        return (SolidColorBrush)(new BrushConverter().ConvertFrom(HexColorString));
    }    
}

Then use like this: BackColor = "#FFADD8E6".ToBrush()

Alternately if you could provide a method to do the same thing.

public SolidColorBrush BrushFromHex(string hexColorString)
{
    return (SolidColorBrush)(new BrushConverter().ConvertFrom(hexColorString));
}

BackColor = BrushFromHex("#FFADD8E6");

Solution 6 - Wpf

vb.net version

Me.Background = CType(New BrushConverter().ConvertFrom("#ffaacc"), SolidColorBrush)

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
QuestionMahesha999View Question on Stackoverflow
Solution 1 - WpfChris RayView Answer on Stackoverflow
Solution 2 - WpfJon VielhaberView Answer on Stackoverflow
Solution 3 - WpfGJHixView Answer on Stackoverflow
Solution 4 - WpfMahesha999View Answer on Stackoverflow
Solution 5 - WpfNeil BView Answer on Stackoverflow
Solution 6 - WpfDr. SomebodyView Answer on Stackoverflow