Working with Brushes in WPF PART:6 in VB.NET

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

Brushes: As you learn in my previous article part 1, part 2, part 3, part 4, part 5 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 VisualBrush.

VisualBrush: A VisualBrush paints an area with a Visual. It is an unusual brush that allows you to take the visual content of an element and use it to fill any surface. Like using a VisualBrush you could copy the appearance of a button in a window to a region somewhere else in that same window. However, the button copy won't be clickable or interactive in any way. A VisualBrush also enables you to project content from one portion of your application into another area; it's very useful for creating reflection effects and magnifying portions of the screen.

Example of an VisualBrush
Xaml Code

<Window x:Class="MainWindow"
    Title="MainWindow" Height="350" Width="525">
    <Rectangle Width="150" Height="150">
            <VisualBrush TileMode="Tile">
                                                <GradientStop Color="Blue" Offset="0.1" />
                                                <GradientStop Color="Green" Offset="1.0" />
                                                <RectangleGeometry Rect="0,0,50,50" />
                                                <RectangleGeometry Rect="50,50,50,50" />
                        <TextBlock FontSize="10pt" Margin="10">MANISH</TextBlock>

VB Code

    Dim Rectangle6 As New Rectangle()
        Rectangle6.Width = 150
        Rectangle6.Height = 150
    ' Create a VisualBrush and use it
    ' to paint the rectangle.
    Dim Brush As New VisualBrush() 
    ' Create the brush's contents.
    Dim Panel As New StackPanel()
    ' Create a DrawingBrush and use it to
    ' paint the panel.
    Dim myBrush As New DrawingBrush()
    Dim myGroup As New GeometryGroup()
        myGroup.Children.Add(New RectangleGeometry(New Rect(0, 0, 50, 50)))
        myGroup.Children.Add(New RectangleGeometry(New Rect(50, 50, 50, 50)))
    Dim checkerBrush As New RadialGradientBrush()
        checkerBrush.GradientStops.Add(New GradientStop(Colors.Blue, 0.1))
        checkerBrush.GradientStops.Add(New GradientStop(, 1.0))
    Dim checkers As New GeometryDrawing(checkerBrush, Nothing, myGroup)
        myBrush.Drawing = checkers
        aPanel.Background = myDrawingBrushBrush
    ' Create some text.
    Dim someText As New TextBlock()
        someText.Text = "Hello, World"
    Dim fSizeConverter As New FontSizeConverter()
        someText.FontSize = CDbl(fSizeConverter.ConvertFromString("10pt"))
        someText.Margin = New Thickness(10)
        myBrush.Visual = aPanel
        Rectangle6.Fill = myBrush

Output Window



Hope this article will help you understand the working of VisualBrush in WPF.


More Articles

© 2020 DotNetHeaven. All rights reserved.