wpfsilverlightbrushbrushesimagebrush

Create Custom Brush With Png(transparent) Image and a Background Color


I want to create a custom brush in WPF that will be applied to a rectangle. Fill= myCustomBrush. theCustomBrush contains a png image (ImageBrush) ,and contanins a color in background (SolidColorBrush). I want to create a custombrush with this two standards brushes.

Thanks for Help.


Solution

  • You can use VisualBrush for this. Example

    <SolidColorBrush x:Key="MyBackgroundColor" Color="Green"/>
    <VisualBrush x:Key="RectangleBrush">
        <VisualBrush.Visual>
            <Grid>
                <Rectangle Fill="{StaticResource MyBackgroundColor}"/>
                <Image Source="BackgroundImageSource"/>
            </Grid>
        </VisualBrush.Visual>
    </VisualBrush>
    
    <!-- ... -->
    
    <Rectangle Fill="{StaticResource RectangleBrush}"/>