1

I'm searching for the best method to make pattern for background of canvas. I want to show user canvas with drawed lines, which will show borders for different size of document, e.g. Paper A4. I'v made it by using DrawingBrush in XAML. Here is my code:

<DrawingBrush x:Key="BackgroundPattern" ViewportUnits="Absolute" Stretch="None" TileMode="Tile">
        <DrawingBrush.Viewport>
            <Rect X="0" Y="0" Width="1089" Height="1842"/>
        </DrawingBrush.Viewport>
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="Blue">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup>
                            <RectangleGeometry>
                                <RectangleGeometry.Rect>
                                    <Rect X="0" Y="0" Width="1089" Height="1842"/>
                                </RectangleGeometry.Rect>
                            </RectangleGeometry>
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="#FFB9B9B9">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup>
                            <RectangleGeometry>
                                <RectangleGeometry.Rect>
                                    <Rect X="1" Y="1" Width="1088" Height="1841"/>
                                </RectangleGeometry.Rect>
                            </RectangleGeometry>
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>

I'm using this brush in tile mode, there is effect: enter image description here

I'm afraid of the size of my elements - Viewport size is 1089x1842. Is there any other way to make these?

1 Answer 1

2

Although the size of a WPF Drawing shouldn't matter (because it is not a bitmap), you could probably simplify it like this:

<DrawingBrush x:Key="BackgroundPattern" ViewportUnits="Absolute" Viewport="0,0,1089,1842"
    AlignmentX="Left" AlignmentY="Top" Stretch="None" TileMode="Tile">
    <DrawingBrush.Drawing>
        <GeometryDrawing Brush="#FFB9B9B9">
            <GeometryDrawing.Pen>
                <Pen Thickness="1" Brush="Blue"/>
            </GeometryDrawing.Pen>
            <GeometryDrawing.Geometry>
                <RectangleGeometry Rect="0,0,1089,1842"/>
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>
Sign up to request clarification or add additional context in comments.

2 Comments

So it is better to use WPF Drawing than put and repeat bitmap?
Sure, but your approach was already a drawing. I was just referring to your "I'm afraid of the size of my elements".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.