Visual Brush in WPF

This sample shows how to use Visual Brush in WPF.
  • 6148

In this article describes how to use VisualBrush in WPF. VisualBrush is a new addition to the brush types since GDI+.  A VisualBrush is used to fill UI elements. The following code uses a VisualBrush to fill a rectangle. I will cover VisualBrush object, its methods and properties in more details in my seperate article.

With a VisualBrush you can define a simple or complex UI element and assign it to the VisualBrush.Visual property.  Then you can paint other parts of your screen with this conglomerate brush.  You get a number of performance benefits from the VisualBrush because WPF can use a copy of the the pixels it has already rendered.

VisualBrush has five TileMode properties:

1.       FlipX

2.       FlipY

3.       FlipXY

4.       None

5.       Tile

FlipX:

<Window x:Class="WPF_VisualBrush.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="VisualBrush in WPF"  Height="400" Width="400">

    <Window.Background>

        <VisualBrush TileMode="FlipX"  Viewport="0,0,0.5,0.5">

            <VisualBrush.Visual>              

                <Image Source="Raj 039.JPG"></Image>

            </VisualBrush.Visual>

        </VisualBrush>

    </Window.Background>

</Window>

 

 

untitled.JPG

Figure1

FlipY:

<Window x:Class="WPF_VisualBrush.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="VisualBrush in WPF"  Height="400" Width="400">

    <Window.Background>

        <VisualBrush TileMode="FlipY"  Viewport="0,0,0.5,0.5">

            <VisualBrush.Visual>              

                <Image Source="Raj 039.JPG"></Image>

            </VisualBrush.Visual>

        </VisualBrush>

    </Window.Background>

</Window>

 

 

untitled1.JPG


Figure2.

 

FlipXY:

<Window x:Class="WPF_VisualBrush.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="VisualBrush in WPF"  Height="400" Width="400">

    <Window.Background>

        <VisualBrush TileMode="FlipXY"  Viewport="0,0,0.5,0.5">

            <VisualBrush.Visual>              

                <Image Source="Raj 039.JPG"></Image>

            </VisualBrush.Visual>

        </VisualBrush>

    </Window.Background>

</Window>

 

untitled2.JPG

Figure3.

None:

<Window x:Class="WPF_VisualBrush.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="VisualBrush in WPF"  Height="400" Width="400">

    <Window.Background>

        <VisualBrush TileMode="None"  Viewport="0,0,0.5,0.5">

            <VisualBrush.Visual>              

                <Image Source="Raj 039.JPG"></Image>

            </VisualBrush.Visual>

        </VisualBrush>

    </Window.Background>

</Window>

 

untitled3.JPG

 

Figure4.

 

 

Tile:

 

<Window x:Class="WPF_VisualBrush.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="VisualBrush in WPF"  Height="400" Width="400">

    <Window.Background>

        <VisualBrush TileMode="Tile"  Viewport="0,0,0.5,0.5">

            <VisualBrush.Visual>              

                <Image Source="Raj 039.JPG"></Image>

            </VisualBrush.Visual>

        </VisualBrush>

    </Window.Background>

</Window>

 

 

 

untitled4.JPG


Figure5.

 

One more example:

 

<Window x:Class="WPF_VisualBrush.Window2"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window2">

    <DockPanel>

        <StackPanel Margin="5" x:Name="Panel">

            <Button>Button</Button>

            <CheckBox>CheckBox</CheckBox>

            <TextBox></TextBox>

        </StackPanel>

        <Rectangle>

            <Rectangle.Fill>

                <VisualBrush TileMode="FlipXY" Viewport="0,0,0.5,0.5" Visual="{Binding ElementName=Panel}"></VisualBrush>

            </Rectangle.Fill>

        </Rectangle>

    </DockPanel>

</Window>

 

untitled5.JPG


Output:

 

This is it.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.