Visual Brush In WPF

In this article describes how to use a VisualBrush in WPF. VisualBrush is a new addition to the brush types since GDI+. A VisualBrush is used to fill UI elements.

Introduction

This article describes how to use a VisualBrush in WPF. VisualBrush is a new addition to the brush types since GDI+. A VisualBrush is used to fill UI elements.

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

Getting Started

The VisualBrush element of XAML represents a Visual Brush. You treat this brush the same as you treat other brushes. For example, you can set this brush as a background property of a window or a control. The TileMode property is used to set the tile of the brush. The Visual property of VisualBrush sets the visual and it can be an image. For example, the following code snippet creates a VisualBrush and sets its FlipX and Visual properties.

  1. <VisualBrushTileMode="FlipX"Viewport="0,0,0.5,0.5">  
  2.     <VisualBrush.Visual>  
  3.         <ImageSource="Raj 039.JPG">  
  4.             </Image>  
  5.     </VisualBrush.Visual>  
  6. </VisualBrush>  

The following code snippet sets the visual property to an Image.

FlipX

  1. <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">  
  2.     <Window.Background>  
  3.         <VisualBrushTileMode="FlipX"Viewport="0,0,0.5,0.5">  
  4.             <VisualBrush.Visual>  
  5.                 <ImageSource="Raj 039.JPG">  
  6.                     </Image>  
  7.             </VisualBrush.Visual>  
  8.             </VisualBrush>  
  9.     </Window.Background>  
  10. </Window> 

The output looks like Figure 1.

Visual Brush In WPF
Figure 1

FlipY

  1. <Windowx: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">  
  2.     <Window.Background>  
  3.         <VisualBrushTileMode="FlipY"Viewport="0,0,0.5,0.5">  
  4.             <VisualBrush.Visual>  
  5.                 <ImageSource="Raj 039.JPG">  
  6.                     </Image>  
  7.             </VisualBrush.Visual>  
  8.             </VisualBrush>  
  9.     </Window.Background>  
  10. </Window> 

The FlipY output looks like Figure 2.

FlipY Output
Figure 2.

FlipXY

  1. <Windowx: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">  
  2.     <Window.Background>  
  3.         <VisualBrushTileMode="FlipXY"Viewport="0,0,0.5,0.5">  
  4.             <VisualBrush.Visual>  
  5.                 <ImageSource="Raj 039.JPG">  
  6.                     </Image>  
  7.             </VisualBrush.Visual>  
  8.             </VisualBrush>  
  9.     </Window.Background>  
  10.  </Window> 
The FlipXY output looks like Figure 3.

FlipXY Output
Figure 3.

None
  1. <Windowx: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">  
  2.     <Window.Background>  
  3.         <VisualBrushTileMode="None"Viewport="0,0,0.5,0.5">  
  4.             <VisualBrush.Visual>  
  5.                 <ImageSource="Raj 039.JPG">  
  6.                     </Image>  
  7.             </VisualBrush.Visual>  
  8.             </VisualBrush>  
  9.     </Window.Background>  
  10. </Window>  

The None (no visual brush) output looks like Figure 4.

No Visual Brush Output
Figure 4.

Tile
  1. <Windowx: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">  
  2.     <Window.Background>  
  3.         <VisualBrushTileMode="Tile"Viewport="0,0,0.5,0.5">  
  4.             <VisualBrush.Visual>  
  5.                 <ImageSource="Raj 039.JPG">  
  6.                     </Image>  
  7.             </VisualBrush.Visual>  
  8.             </VisualBrush>  
  9.     </Window.Background>  
  10. </Window> 

The Tile output looks like Figure 5.

Tile output
Figure 5.

The Visual Brush can be applied to any control. It doesn't have to be Windows as shown in my previous example. It can be a button, a rectangle, or a TextBox.

Let's see one more example with a Rectangle. In the following code snippet, I will use a rectangle with a Visual Brush and set its TileMode property.

  1. <Windowx:Class="WPF_VisualBrush.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window2">  
  2.     <DockPanel>  
  3.         <StackPanelMargin="5"x:Name="Panel">  
  4.             <Button>Button</Button>  
  5.             <CheckBox>CheckBox</CheckBox>  
  6.             <TextBox></TextBox>  
  7.             </StackPanel>  
  8.             <Rectangle>  
  9.                 <Rectangle.Fill>  
  10.                     <VisualBrushTileMode="FlipXY"Viewport="0,0,0.5,0.5"Visual=" {Binding ElementName=Panel}">  
  11.                         </VisualBrush>  
  12.                 </Rectangle.Fill>  
  13.             </Rectangle>  
  14.     </DockPanel>  
  15.  </Window>  
The output generates the following Figure.

 Visual Brush In WPF - Output

Summary

In this article, we discussed how to use a VisualBrush in WPF.