ARTICLE

How to use Visual Brush in WPF

Posted by Raj Kumar Articles | WPF using VB.NET January 30, 2009
This sample shows how to use Visual Brush in WPF.
 
Reader Level:

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>

 

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>

 

 


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>

 

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>

 

 

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>

 

 

 


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>

 


Output:

 

 

This is it.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor