Bruno Machado - vargero
Bruno Machado - vargero

Reputation: 2730

Is it possible to change an ImageBrush color inside WPF?

I'm trying to do something quite simple, but I'm not sure if it's possible. I have this image:

Tile target

Inside my board game project, I use this to indicate the mouse position in the board and stuff like that (being the board a 8x8 grid with a rectangle inside). I'm also setting the grid cell background brush as, say, a SolidColorBrush with Blue color and .25 opacity, while the rectangle that lies above it with the target, or vice-versa, because I couldn't achieve my desired effect, which is, the part that is black to turn full blue, as my brush, while the rest remains with the opacity and etc. The way I did, it was possible to make the black part become blueish, with a transluscent background, but the color in the border isn't quite right yet:

enter image description here

I don't know if I was able to make myself clear, but I guess the picture shows what is my goal.

The code:

            ImageBrush targetBrush = new ImageBrush();
            targetBrush.ImageSource = new BitmapImage(new Uri("Resources/GameScreen/selectedTile.png", UriKind.Relative));
            targetBrush.Opacity = 1;

            SolidColorBrush backBrush = new SolidColorBrush(Colors.Blue);
            backBrush.Opacity = .25;

            tile.Children.Add(new Rectangle());
            foreach (object target in tile.Children)
            {
                if (target.GetType() == typeof(Rectangle))
                {

                    ((Rectangle)target).Fill = backBrush;
                }
            }



            tile.Background = targetBrush;

Thanks in advance.

Upvotes: 1

Views: 2846

Answers (1)

brunnerh
brunnerh

Reputation: 185290

As the image is just a mask you could use a Rectangle with a blue Fill and an OpacityMask set to your image.

Upvotes: 2

Related Questions