AlpakaJoe
AlpakaJoe

Reputation: 593

Get RGB-colors of a filled rectangle in WPF

In my WPF project i have a rectangle. The fill-Color of the rectangle changes during runtime.

If the user clicks on the rectangle, he should get rgb-values of that rectangle.

I know that i can save it as Brush like this:

Brush brush = rectangle.Fill;

But i don't know how to get the RGB-values out of that?

what i would need is someting like:

labelRed.Text = brush.red;    
labelGreen.Text = brush.green;    
labelBlue.Text = brush.blue;

Upvotes: 2

Views: 1148

Answers (1)

Juan Salvador Portugal
Juan Salvador Portugal

Reputation: 1319

You should get the SolidColorBrush from the Fill property, then get the Color struct from the SolidColorBrush, now the Color object, has the R G and B property

SolidColorBrush solidColorBrush = rectangle.Fill as SolidColorBrush;

if (solidColorBrush != null)
{
    Color color = solidColorBrush.Color;
    byte r = color.R;
    byte g = color.G;
    byte b = color.B;

    MessageBox.Show($"R{r}\nG{g}\nB{b}");
}

Upvotes: 2

Related Questions