Wednesday, June 3, 2009

How to convert a Brush to Color in C#

Or How to get the Color of a Brush object in .NET

The solution is pretty simple:
  
 new Pen(brush).Color

The reverse is also possible

 new SolidBrush(color)

10 comments:

Unknown said...

Thank You! You help me...

Anonymous said...

Pretty simple...

...and pretty ugly an' all :-) Shame on Microsoft for not exposing the brush's properties as nicely as the Pen.

Still, >you< got me out of a big hole -- thanks!

(Makes me wonder about gradient brushes for a single colour though; Might have to see what the pen does with that...)

Abhishek Chatterjee said...

Thanks for posting this! On a bad day something as simple as this could hold you down...

LK said...

...like it did to me today! Thank you very much!

dickdinh said...

awesome thanks. This saved me today, but in my case I have to do something like since it's Silverlight:

SolidColorBrush S = txtColorWord1.Foreground as SolidColorBrush;

txtBlock.Text = S.Color.ToString();

Unknown said...

Thanks for that, was getting my hair off with the Brush!

Anonymous said...

Thanks for that simple solution! It's a bit silly that it isn't possible to get it directly.

Mike Garrett said...

It's not silly at all. Only solid brushes even have a colour. Other brushes can paint with gradients or bitmaps or whatever.

You can use:
Color colour = ((SolidBrush)my_brush).Color;

joy said...

Hi, I hope you have a good day. Thank you for inspiring us. You bring joy to our face.Visit my site if you have time.

n8fan.net

www.n8fan.net

jorgeefrrr828 said...

An interesting discussion is worth comment. I think that you should write more on this subject, it might not be a taboo subject but usually people are not enough to talk on such topics. To the next. Cheers casino blackjack