Working with Brushes WPF: Part 5 in VB.NET

In this article you learn about the all types of brushes and how to working with them in WPF.
  • 2072

 Brushes: As you learn in my previous article part 1, part 2, part 3, part 4 a brush is used to describe the background of a button, the foreground of text, and the fill of a shape and all brushes classes are inherit from System.Windows.Media.Brush and give you more exotic effects.

Here we discuss about the third Brush class that is DrawingBrush.

DrawingBrush: A DrawingBrush paints an area with a Drawing. A Drawing can contain shapes, images, text, and media.

Example of an DrawingBrush
Xaml Code

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width
="525">
    <Rectangle Width="150" Height="150">
        <Rectangle.Fill>
            <DrawingBrush Viewport="0,0,0.25,0.25" TileMode="Tile">
                <DrawingBrush.Drawing>
                    <DrawingGroup>
                        <GeometryDrawing Brush="White">
                            <GeometryDrawing.Geometry>
                                <RectangleGeometry Rect="2,0,200,300" />
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                        <GeometryDrawing>
                            <GeometryDrawing.Geometry>
                                <GeometryGroup>
                                    <RectangleGeometry Rect="0,0,100,100" />
                                    <RectangleGeometry Rect="50,50,50,50" />
                                </GeometryGroup>
                            </GeometryDrawing.Geometry>
                            <GeometryDrawing.Brush>
                                <LinearGradientBrush>
                                    <GradientStop Offset="0.2" Color="Blue" />
                                    <GradientStop Offset="0.3" Color="Red" />
                                    <GradientStop Offset="2.0" Color="Pink" />
                                </LinearGradientBrush>
                            </GeometryDrawing.Brush>
                        </GeometryDrawing>
                    </DrawingGroup>
                </DrawingBrush.Drawing>  
          
</DrawingBrush>
        </Rectangle.Fill>
    </Rectangle>
</
Window>

VB Code

        
Dim Rectangle5 As New Rectangle()
         Rectangle5.Width = 150
         Rectangle5.Height = 150
    ' Create a DrawingBrush and use it to
    ' paint the rectangle.
    Dim myBrush As New DrawingBrush()
    Dim backgroundSquare As New GeometryDrawing(Brushes.White, NothingNewRectangleGeometry(New Rect(2, 0, 200, 300)))
    Dim Geometry As New GeometryGroup()
        Geometry.Children.Add(New RectangleGeometry(New Rect(0, 0, 100, 100)))
        Geometry.Children.Add(New RectangleGeometry(New Rect(50, 50, 50, 50)))
    Dim Brush As New LinearGradientBrush()
        Brush.GradientStops.Add(New GradientStop(Colors.Black, 0.2))
        Brush.GradientStops.Add(New GradientStop(Colors.Gray, 0.2))
        Brush.GradientStops.Add(New GradientStop(Colors.Gray, 2.0))
    Dim checkers As New GeometryDrawing(Brush, Nothing, Geometry)
    Dim DrawingGroup As New DrawingGroup()
        DrawingGroup.Children.Add(backgroundSquare)
        DrawingGroup.Children.Add(checkers) 
        Brush.Drawing = checkersDrawingGroup
        Brush.Viewport = New Rect(0, 0, 0.25, 0.25)
        Brush.TileMode = TileMode.Tile 
        Rectangle5.Fill = myBrush 

Output Window

brush5.gif
Conclusion

Hope this article will help you understand the working of DrawingBrush in WPF. Remaining types of brushes will explain in my next articles.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.