none
Convert color name to brush using C#?

    Question

  • Hi there,

     

    Using the old System.Drawing.Color.FromName method I can create a System.Drawing.Color object.

     

    What if I would like to do the same, but have a System.Windows.Media.SolidColorBrush as result?

     

    One way I could image is first create a S.D.Color object using the method above, then create a S.W.M.Color object using the ARGB values from the first and pass that as parameter to create a S.W.M.SolidColorBrush, but that would be kinda overkill wouldn't it?

     

    If possible, I would also like to have this work if the input string is a hex-value instead.

     

    Kind regards,

    Monday, February 11, 2008 4:46 PM

Answers

  • Use BrushConverter from the System.ComponentModel namespace:

     

    BrushConverter conv = new BrushConverter();

     

    You can use a color name:

     

    SolidColorBrush brush = conv.ConvertFromString("Red") as SolidColorBrush;

     

    You can also use an RGB value:

     

    SolidColorBrush brush = conv.ConvertFromString("#0000FF") as SolidColorBrush;

     

    I suspect you can use anything you can use in XAML.

    Thursday, February 14, 2008 12:34 AM

All replies

  • You could probably just use the TypeConverter associated with brushes - BrushConverter. Try its ConvertFrom method, passing the strings of your choice.
    Monday, February 11, 2008 5:08 PM
  • Hi... Another option is to call Colors.GetProperties(). The resultant PropertyInfo[] returned holds Color objects that have all the information you need about colors - including the name, RGB values, etc.

     

    jpuopolo

     

     

    • Proposed as answer by .peter.gfader Monday, June 18, 2012 1:01 PM
    Monday, February 11, 2008 7:47 PM
  • There is no Colors.GetProperties() for me, atleast not in System.Windows.Media.Colors

    Tuesday, February 12, 2008 10:38 PM
  • I think that you can use reflection to do this. If you refer to the MSDN library for Brushes class, you will find that all named brushes are SolidColorBrush. If user input string is the one of brush names, you can use reflection to get the brush from the Brushes class. If user input string is a hex-value, you can loop all brushes in the Brushes class, and get the SolidColorBrush.Color property to get the corresponding brush.

     

    Best Regards,

    Wei Zhou

    Wednesday, February 13, 2008 5:59 AM
    Moderator
  • Use BrushConverter from the System.ComponentModel namespace:

     

    BrushConverter conv = new BrushConverter();

     

    You can use a color name:

     

    SolidColorBrush brush = conv.ConvertFromString("Red") as SolidColorBrush;

     

    You can also use an RGB value:

     

    SolidColorBrush brush = conv.ConvertFromString("#0000FF") as SolidColorBrush;

     

    I suspect you can use anything you can use in XAML.

    Thursday, February 14, 2008 12:34 AM
  •  

    Way don't I see the object BrushConverter?

     

    I am using vs2005

    Thursday, February 28, 2008 1:18 PM
  • I said BrushConverter was in the System.ComponentModel namespace. It's actually in System.Windows.Media.  Sorry for the confusion.

     

    Thursday, February 28, 2008 1:37 PM
  • You forgot to write that it's only from .Net 3

     

    Thier is another way to do it

    SolidBrush s = new SolidBrush(Color.Red)

     

    • Proposed as answer by George Breeze Monday, December 14, 2009 10:41 AM
    Thursday, February 28, 2008 1:42 PM
  • Hi,

    Either you can convert the Color using Hex string like, 

    Color myColor = (Color)ColorConverter.ConvertFromString("#FFDFD991");

    Then you can just convert Brushes using Media.Brushes.

    Regards,

    Suriya.


    sunmoon_1411
    Tuesday, June 1, 2010 8:42 AM