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.