Convert string to Brushes/Brush color name in C#
Solution 1
Recap of all previous answers, different ways to convert a string to a Color or Brush:
// best, using Color's static method
Color red1 = Color.FromName("Red");
// using a ColorConverter
TypeConverter tc1 = TypeDescriptor.GetConverter(typeof(Color)); // ..or..
TypeConverter tc2 = new ColorConverter();
Color red2 = (Color)tc.ConvertFromString("Red");
// using Reflection on Color or Brush
Color red3 = (Color)typeof(Color).GetProperty("Red").GetValue(null, null);
// in WPF you can use a BrushConverter
SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");
Solution 2
String to brush:
myTextBlock.Foreground = new BrushConverter().ConvertFromString("#FFFFFF") as SolidColorBrush;
That's my case here!
Solution 3
A brush can be declared like this
Brush myBrush = new SolidBrush(Color.FromName("Red"));
Solution 4
D'oh. After a while of looking I found:
Color.FromName(a.Value)
After hitting "post". From there it's a short step to:
color = new SolidBrush(Color.FromName(a.Value));
I'll leave this question here for others....
Solution 5
You could use reflection for this:
Type t = typeof(Brushes);
Brush b = (Brush)t.GetProperty("Red").GetValue(null, null);
Of course, you'll want some error handling/range checking if the string is wrong.
Clinton Pierce
Professionally programming since I was 15 or so. Not a language bigot -- after a while, they all look the same. Published some Perl books back in the dot-com heyday. You may remember me from such conferences as YAPC, The Perl Conference (before they were OSCON!), and USENIX LISA. Any code posted to this site by me should be considered in the Public Domain unless otherwise noted.
Updated on July 09, 2022Comments
-
Clinton Pierce almost 2 years
I have a configuration file where a developer can specify a text color by passing in a string:
<text value="Hello, World" color="Red"/>
Rather than have a gigantic switch statement look for all of the possible colors, it'd be nice to just use the properties in the class System.Drawing.Brushes instead so internally I can say something like:
Brush color = Brushes.Black; // Default // later on... this.color = (Brush)Enum.Parse(typeof(Brush), prasedValue("color"));
Except that the values in Brush/Brushes aren't enums. So Enum.Parse gives me no joy. Suggestions?
-
Lucas almost 15 yearsNote that Color and Brush is not the same thing, you seem to be mixing them up
-
-
Jon B over 15 yearsStrictly speaking, it's a "coincidence" that the static properties on Brushes use the same name as the static properties on Color. However, that's probably nothing to worry about.
-
Lucas almost 15 yearsSystem.Drawing.ColorTranslator has FromHtml() and ToHtml()
-
Lucas almost 15 yearsTypeDescriptor cannot convert from string to Brush. It can convert string to Color, though...
-
Lucas almost 15 yearsAlso note that this requires .Net 3.0 or later
-
John M over 14 years+1 An alternative is: Brush aBrush = new SolidBrush(Color.FromArgb(240, 240, 240));
-
Chuck Savage almost 11 yearsMind adding how you'd do this with WPF's XAML as well?
-
Matt about 9 yearsI get this error:
Error 1 'System.Windows.Media.Color' does not contain a definition for 'FromName'
VS Express 2012, and I am in a standard WPF application codebehind. -
escape-llc almost 9 years@Matt FromColor() is a member of
Windows.Drawing.Color
which is not a WPF class. -
Sasha Yakobchuk over 5 yearsUnlike other answers, this one does not create new brush instances, which is good, see stackoverflow.com/questions/40904567/…