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

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"
    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>
            <VisualBrush TileMode="Tile">
                <VisualBrush.Visual>
                    <StackPanel>
                        <StackPanel.Background>
                            <DrawingBrush>
                                <DrawingBrush.Drawing>
                                    <GeometryDrawing>
                                        <GeometryDrawing.Brush>
                                            <RadialGradientBrush>
                                                <GradientStop Color="Blue" Offset="0.1" />
                                                <GradientStop Color="Green" Offset="1.0" />
                                            </RadialGradientBrush>
                                        </GeometryDrawing.Brush>
                                        <GeometryDrawing.Geometry>
                                            <GeometryGroup>
                                                <RectangleGeometry Rect="0,0,50,50" />
                                                <RectangleGeometry Rect="50,50,50,50" />
                                            </GeometryGroup>
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                </DrawingBrush.Drawing>
                            </DrawingBrush>
                        </StackPanel.Background>
                        <TextBlock FontSize="10pt" Margin="10">MANISH</TextBlock>
                    </StackPanel>
                </VisualBrush.Visual>
            </VisualBrush>
        </Rectangle.Fill>
    </Rectangle>
</
Window>

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(Colors.green, 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)
        aPanel.Children.Add(someText) 
        myBrush.Visual = aPanel
        Rectangle6.Fill = myBrush

Output Window

brush6.gif
 

Conclusion

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.