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?
Try this:
Brush brush = Brushes.Yellow;
Color color = ((SolidColorBrush) brush).Color;
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With