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.
  • 2854

 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"
    Title="MainWindow" Height="350" Width
    <Rectangle Width="150" Height="150">
            <DrawingBrush Viewport="0,0,0.25,0.25" TileMode="Tile">
                        <GeometryDrawing Brush="White">
                                <RectangleGeometry Rect="2,0,200,300" />
                                    <RectangleGeometry Rect="0,0,100,100" />
                                    <RectangleGeometry Rect="50,50,50,50" />
                                    <GradientStop Offset="0.2" Color="Blue" />
                                    <GradientStop Offset="0.3" Color="Red" />
                                    <GradientStop Offset="2.0" Color="Pink" />

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()
        Brush.Drawing = checkersDrawingGroup
        Brush.Viewport = New Rect(0, 0, 0.25, 0.25)
        Brush.TileMode = TileMode.Tile 
        Rectangle5.Fill = myBrush 

Output Window


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


More Articles

© 2020 DotNetHeaven. All rights reserved.