Using multiple brushes inside one GeometryDrawing in WPF

Can I use multiple brushes inside one GeometryDrawing? I have several geometries that I want to paint with different brushes, and quite verbose to declare an individual GeometryDrawingfor each. I am looking for a more concise way to express the following:

<DrawingImage x:Key="SomeDrawingImage">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="{StaticResource SomeGradient}">
                <GeometryDrawing.Geometry>
                    <PathGeometry Figures="{StaticResource SomeFigures}">
                        <PathGeometry.Transform>
                            <TransformGroup>
                                <TranslateTransform X="50" />
                            </TransformGroup>
                        </PathGeometry.Transform>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
            <GeometryDrawing Brush="{StaticResource SomeOtherGradient}">
                <GeometryDrawing.Geometry>
                    <PathGeometry Figures="{StaticResource SomeOtherFigures}">
                        <PathGeometry.Transform>
                            <TransformGroup>
                                <TranslateTransform X="100" />
                            </TransformGroup>
                        </PathGeometry.Transform>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>
+3
source share
4 answers

As far as I understand your question, I would say it is not possible to have multiple brushes inside one GeometryDrawing .

The whole purpose of GeometryDrawing is to combine the stroke (with the Pen property) and the fill (using the Brush property) ... with the geometry (using the Geometry property).

xaml , ( ), ... xaml .

+4

var source = image.Source as DrawingImage;
var drawing = source.Drawing as DrawingGroup;
foreach (GeometryDrawing geometryDrawing in drawing.Children)
{
    geometryDrawing.Brush = Brushes.Brown;
}
0

You can always overwrite brush resources when using GeometryDrawing:

<Image Source="{StaticResource SomeDrawingImage}">
    <Image.Resources>
        <SolidColorBrush x:Key="SomeGradient" Color="Brown" />
        <SolidColorBrush x:Key="SomeOtherGradient" Color="Yellow" />
    </Image.Resources>
</Image>
0
source

You can use a visual brush to achieve this.

 <Grid.Background>
           <VisualBrush>
                    <VisualBrush.Visual>
                        <Grid 
                            Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FrameworkElement}}, Mode=OneWay}" 
                            Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FrameworkElement}}, Mode=OneWay}">

                            <Rectangle Fill="Blue" />
                            <Image Source="your image path" Stretch="Uniform" />

                        </Grid>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Grid.Background>
0
source

Source: https://habr.com/ru/post/1726346/


All Articles