WPF Drawing Brush

Drawing Brush

The Drawing object in WPF represents a 2-D drawing that includes shapes, text, video, image and other drawings. A Drawing Brush represented by the DrawingBrush object paints a surface with a drawing. The DrawingGroup, GeometryDrawing, GlyphRunDrawing, ImageDrawing, and VideoDrawing classes are inherited from the Drawing class. That means we can create any of these objects to be painted by a DrawingBrush.

Creating a Drawing Brush

The DrawingBrush element in XAML creates a drawing brush.

The following code snippet creates a drawing brush and sets the Drawing property. The Drawing property can be an element inherited from the Drawing such as a GeometryDrawing.

  1. <DrawingBrush>  
  2.     <DrawingBrush.Drawing />  
  3. </DrawingBrush>  

We can fill a shape with a drawing brush by setting a shape's Fill property to the image brush. The code snippet in Listing 23 creates a rectangle shape sets the Fill property to a DrawingBrush.

  1. <Grid Name="LayoutRoot">  
  2.     <Rectangle Width="200" Height="200" Stroke="Black" StrokeThickness="0">  
  3.         <Rectangle.Fill>  
  4.             <DrawingBrush>  
  5.                 <DrawingBrush.Drawing>  
  6.                     <GeometryDrawing Brush="Yellow">  
  7.                         <GeometryDrawing.Geometry>  
  8.                             <GeometryGroup>  
  9.                                 <RectangleGeometry Rect="50,25,25,25" />  
  10.                                 <RectangleGeometry Rect="25,50,25,25" />  
  11.                             </GeometryGroup>  
  12.                         </GeometryDrawing.Geometry>  
  13.                         <GeometryDrawing.Pen>  
  14.                             <Pen Thickness="5">  
  15.                                 <Pen.Brush>  
  16.                                     <LinearGradientBrush>  
  17.                                         <GradientStop Offset="0.0" Color="Blue" />  
  18.                                         <GradientStop Offset="1.0" Color="Black" />  
  19.                                     </LinearGradientBrush>  
  20.                                 </Pen.Brush>  
  21.                             </Pen>  
  22.                         </GeometryDrawing.Pen>  
  23.                     </GeometryDrawing>  
  24.                 </DrawingBrush.Drawing>  
  25.             </DrawingBrush>  
  26.         </Rectangle.Fill>  
  27.     </Rectangle>  
  28. </Grid>  
Listing 23

The output looks like Figure 27.

DBImg1.jpg
Figure 27. A shape filled with a Drawing brush

The Viewport property determines the size and position of the base tile when the TileMode of a DrawingBrush is not set to None, and the ViewportUnits property determines whether the Viewport is specified using absolute or relative coordinates. If the coordinates are relative, they are relative to the size of the output area. The point (0,0) represents the top left corner of the output area, and (1,1) represents the bottom right corner of the output area. To specify that the Viewport property uses absolute coordinates, set the ViewportUnits property to Absolute.

The TileMode property of DrawingBrush represents the tile mode that is a type if a TileMode enumeration. The TileMode enumeration has Tile, FlipX, FlipY, FlipXY, and None values.

The following code snippet sets the Viewport and TileMode properties of a DrawingBrush.

  1. <DrawingBrush Viewport="0,0,0.25, 0.25" TileMode="Tile">  

Figure 28, 29, and 30 show the tile modes values Tile, FilpXY, and FlipX.

A shape filled with a Drawing brush in Tile mode
Figure 28. A shape filled with a Drawing brush in Tile mode

A shape filled with a Drawing brush with TileMode as FlipXY
Figure 29. A shape filled with a Drawing brush with TileMode as FlipXY

A shape filled with a Drawing brush with TileMode as FlipX
Figure 30. A shape filled with a Drawing brush with TileMode as FlipX

One of the key examples of tile mode is creating a chessboard-like image that has a repeating rectangle with black and white background colors. The CreateARectangleWithDrawingBrush method listed in Listing 24 draws a chess board like a rectangle with a drawing brush dynamically.

  1. privatevoid CreateARectangleWithDrawingBrush() {  
  2.     // Create a background recntangle  
  3.     Rectangle chessBoard = newRectangle();  
  4.     chessBoard.Width = 300;  
  5.     chessBoard.Height = 300;  
  6.     // Create a DrawingBrush  
  7.     DrawingBrush blackBrush = newDrawingBrush();  
  8.     // Create a Geometry with white background  
  9.     GeometryDrawing backgroundSquare = newGeometryDrawing(Brushes.White, null, newRectangleGeometry(newRect(0, 0, 400, 400)));  
  10.     // Create a GeometryGroup that will be added to Geometry  
  11.     GeometryGroup gGroup = newGeometryGroup();  
  12.     gGroup.Children.Add(newRectangleGeometry(newRect(0, 0, 200, 200)));  
  13.     gGroup.Children.Add(newRectangleGeometry(newRect(200, 200, 200, 200)));  
  14.     // Create a GeomertyDrawing  
  15.     GeometryDrawing checkers = newGeometryDrawing(newSolidColorBrush(Colors.Black), null, gGroup);  
  16.     DrawingGroup checkersDrawingGroup = newDrawingGroup();  
  17.     checkersDrawingGroup.Children.Add(backgroundSquare);  
  18.     checkersDrawingGroup.Children.Add(checkers);  
  19.     blackBrush.Drawing = checkersDrawingGroup;  
  20.     // Set Viewport and TimeMode  
  21.     blackBrush.Viewport = newRect(0, 0, 0.25, 0.25);  
  22.     blackBrush.TileMode = TileMode.Tile;  
  23.     // Fill rectangle with a DrawingBrush  
  24.     chessBoard.Fill = blackBrush;  
  25.     LayoutRoot.Children.Add(chessBoard);  

Listing 24

The output of Listing 24 looks like Figure 31.

A chess board
Figure 31. A chess board


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.