[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

How Can i Covert String to System.Drawing.Brush

How Can i Covert String to System.Drawing.Brush.

what i want to do is like this:
String  colorString = ConfigurationManager.AppSettings["ColorString "];
System.Drawing.Brush  brush;
how can i convert ColorString  to brush?
0
HanbingCheng
Asked:
HanbingCheng
  • 2
  • 2
  • 2
1 Solution
 
himanshutCommented:
maybe by:

brush = (System.Drawing.Brush)colorString;
0
 
himanshutCommented:
Try this as we;
// 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");

Open in new window

0
 
Miguel OzSoftware EngineerCommented:
Assuming is a SolidBrush:
http://msdn.microsoft.com/en-us/library/system.drawing.solidbrush.solidbrush.aspx

your code should look like:
Color customColor = Color.FromName(colorString);
System.Drawing.Brush  brush = new SolidBrush(customColor);

Note: your string must be a valid color name (http://msdn.microsoft.com/en-us/library/system.drawing.color.fromname.aspx )
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HanbingChengAuthor Commented:
We can use like this:
Brush brush = new SolidBrush(Color.FromName("Red"));
0
 
HanbingChengAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for HanbingCheng's comment http:/Q_27417715.html#37142999

for the following reason:

find a solution by myself
0
 
Miguel OzSoftware EngineerCommented:
Your posted solution is the same as mine. You just did it in one and substitute colorString with "Red". My points please
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now