Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Winforms ControlPaint.Light() in WPF project

I have a Brush object that I want to lighten using the Windows Forms ControlPaint.Light() method.

I want to convert a System.Windows.Media.Brush object to System.Drawing.Color so that I can change any XAML button background color to its "light color" on button click, because System.Windows.Forms.ControlPaint.Light() takes only color as argument.

Since my Brush object is not a SolidColorBrush and so does not have a Color property, is there some alternative to achieve this?

like image 975
Robins Gupta Avatar asked Feb 22 '14 23:02

Robins Gupta


2 Answers

Try this:

        Brush brush = Brushes.Yellow;
        Color color = ((SolidColorBrush) brush).Color;
like image 175
Витёк Синёв Avatar answered Sep 19 '22 22:09

Витёк Синёв


You could try getting the A,RGB values of the brush, and then pass them to System.Drawing.Color.FromARGB() Pseudo-Code:

Brush br = Brushes.Green;
byte a = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).A;
byte g = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).G;
byte r = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).R;
byte b = ((Color)br.GetValue(SolidColorBrush.ColorProperty)).B;
System.Windows.Forms.ControlPaint.Light(
    System.Drawing.Color.FromArgb((int)a,(int)r,(int)g,(int)b));

I'm not a WPF expert, but the main thing I think you need to keep in mind is the easiest way to do what you are trying is to use System.Drawing.Color.FromArgb() or even System.Drawing.Color.FromName().

like image 43
davidsbro Avatar answered Sep 17 '22 22:09

davidsbro