Convert System.Windows.Media.Brush to System.Windows.Media.Color and vice versa

I am working on a project that uses C# and WPF and I have been having one hella time on one small technical problem: converting between different classes of colors. All I needed to do was convert from a System.Windows.Media.Brush to System.Windows.Media.Color in one part of my code and then convert from my System.Windows.Media.Color back to a System.Windows.Media.Brush in another part of the code. Needless to say, it was easy to go from Color to Brush but not from Brush to Color (I searched high and low using Google but had zero luck – hence my post on the topic!).

For those out there who need it:
Color -> Brush
Color newColor = Colors.Blue;
Brush imageColor = new SolidColorBrush(newColor);

Brush -> Color
Brush newColor = Brushes.Blue;
SolidColorBrush newBrush = (SolidColorBrush)newColor;
Color imageColor = newBrush.Color;

Also, a ScatterView does not like to scale Canvas objects.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.