WPF Image Brush in VB.NET

An image brush paints an area with an image. The ImageSource property represents the image to be used during the painting by an image brush. The ImageBrush object represents an image brush.
  • 2687

Image Brush

An image brush paints an area with an image. The ImageSource property represents the image to be used during the painting by an image brush. The ImageBrush object represents an image brush. 

Creating an Image Brush

The ImageBrush element in XAML creates an image brush. The ImageSource property of the ImageBrush represents the image used in the painting process.

The following code snippet creates an image brush and sets the ImageSource property to an image.

<ImageBrush ImageSource="dock.jpg" />

We can fill a shape with an image brush by setting a shape's Fill property to the image brush. The code snippet in Listing 21 creates a rectangle shape sets the Fill property to an ImageBrush.

<Rectangle
    Width="200"
    Height="100"
    Stroke="Black"
    StrokeThickness="4">
    <Rectangle.Fill>
        <ImageBrush ImageSource="dock.jpg" />
    </Rectangle.Fill>
</Rectangle>

Listing 21

The output looks like Figure 26.

ImageBrushImg1.jpg

Figure 26. A shape filled with an image brush

The CreateAnImageBrush method listed in Listing 22 draws same rectangle with an image brush in Figure 26 dynamically.

    ''' <summary>
    ''' Fills a rectangle with an ImageBrush
    ''' </summary>
    Public Sub CreateAnImageBrush()
        ' Create a Rectangle
        Dim blueRectangle As New Rectangle()
        blueRectangle.Height = 100
        blueRectangle.Width = 200

        ' Create an ImageBrush
        Dim imgBrush As New ImageBrush()

        imgBrush.ImageSource = New BitmapImage(New Uri("Dock.jpg", UriKind.Relative))

        ' Fill rectangle with an ImageBrush
        blueRectangle.Fill = imgBrush

        ' Add Rectangle to the Grid.
        LayoutRoot.Children.Add(blueRectangle)
    End Sub

Listing 22

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.