sourcenouveau
sourcenouveau

Reputation: 30504

Using multiple brushes within a single GeometryDrawing in WPF

Is it possible to use multiple brushes within a single GeometryDrawing? I have several geometries that I want to draw with different brushes, and it's rather verbose to have to declare an individual GeometryDrawing for each. I'm 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>

Upvotes: 3

Views: 6429

Answers (4)

Andreas
Andreas

Reputation: 4013

You can use a visualbrush to achieve that

 <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>

Upvotes: 0

cplotts
cplotts

Reputation: 14252

In as far as I understand your question, I would say it is not possible to have multiple brushes within a single GeometryDrawing.

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

To make our xaml more concise, we ourselves have shared not only brushes (which is common) but also geometry ... but your xaml suggests that you are doing the same.

Upvotes: 4

Arcturus
Arcturus

Reputation: 27055

You can always overwrite the resources of the brushes when you use the GeometryDrawing:

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

Upvotes: 0

viky
viky

Reputation: 17659

you can do that in code behind

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

Upvotes: 1

Related Questions