Patterns defined by the DrawingBrush expand to fill the area of the rectangle

image_pdfimage_print


   
 

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
          <Rectangle Width="100" Height="100" Stroke="Black" StrokeThickness="1">
            <Rectangle.Fill>
              <DrawingBrush>
                <DrawingBrush.Drawing>
                  <GeometryDrawing Brush="MediumBlue">
                    <GeometryDrawing.Geometry>
                      <GeometryGroup>
                        <EllipseGeometry RadiusX="0.2" RadiusY="0.45" Center="0.5,0.5" />
                        <EllipseGeometry RadiusX="0.45" RadiusY="0.2" Center="0.5,0.5" />
                      </GeometryGroup>
                    </GeometryDrawing.Geometry>
                    <GeometryDrawing.Pen>
                      <Pen Thickness="0.1" Brush="Black" />
                    </GeometryDrawing.Pen>
                  </GeometryDrawing>
                </DrawingBrush.Drawing>
              </DrawingBrush>
            </Rectangle.Fill>
          </Rectangle>

</Window>

   
     


Non-tiled Drawingrush examples

image_pdfimage_print


   
 
<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
    <Window.Resources>
    <DrawingGroup x:Key="myDrawing">
      <GeometryDrawing>
        <GeometryDrawing.Geometry>
          <GeometryGroup>
            <LineGeometry StartPoint="0,0" EndPoint="1,1" />
            <LineGeometry StartPoint="0,1" EndPoint="1,0" />
          </GeometryGroup>
        </GeometryDrawing.Geometry>
        <GeometryDrawing.Pen>
          <Pen Thickness="0.2" Brush="#3366FF" />
        </GeometryDrawing.Pen>
      </GeometryDrawing>
      <GeometryDrawing Brush="Red"
       Geometry="M 0.4,0.4 L 0.5,0.2 0.6,0.4 0.5,0.5" />
    </DrawingGroup>
    
    </Window.Resources>
    <Rectangle Width="175" Height="90" Stroke="Black">
      <Rectangle.Fill>
        <DrawingBrush Drawing="{StaticResource myDrawing}"></DrawingBrush>
      </Rectangle.Fill>
    </Rectangle>
</Window>

   
     


Tiled DrawingBrush examples

image_pdfimage_print


   
 

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">

    <Window.Resources>
    <DrawingGroup x:Key="myDrawing" >
      <GeometryDrawing>
        <GeometryDrawing.Geometry>
          <GeometryGroup>
            <LineGeometry StartPoint="0,0" EndPoint="1,1" />
            <LineGeometry StartPoint="0,1" EndPoint="1,0" />
          </GeometryGroup>
        </GeometryDrawing.Geometry>
        <GeometryDrawing.Pen>
          <Pen Thickness="0.2" Brush="#3366FF" />
        </GeometryDrawing.Pen>
      </GeometryDrawing>
      <GeometryDrawing Brush="Red"
       Geometry="M 0.4,0.4 L 0.5,0.2 0.6,0.4 0.5,0.5" />
    </DrawingGroup>
    </Window.Resources>
    <Rectangle Width="175" Height="90" Stroke="Black">
      <Rectangle.Fill>
        <DrawingBrush Drawing="{StaticResource myDrawing}" Viewport="0,0,0.5,0.5" TileMode="Tile"></DrawingBrush>
      </Rectangle.Fill>
    </Rectangle>

</Window>

   
     


An ImageDrawing enables a DrawingBrush to contain images

image_pdfimage_print


   
 
<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">
        <Button Height="100" Width="100" Margin="15,0,0,0">Click Me
          <Button.Background>
            <DrawingBrush>
              <DrawingBrush.Drawing>
                <DrawingGroup>
                  <DrawingGroup.Children>
                    <GeometryDrawing Brush="Pink">
                      <GeometryDrawing.Geometry>
                        <RectangleGeometry Rect="0,0,100,100" />
                      </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                    <ImageDrawing ImageSource="c:image.jpg" Rect="50,50,50,50" />
                    <ImageDrawing ImageSource="c:image.jpg" Rect="0,0,50,50" />                      
                  </DrawingGroup.Children>
                </DrawingGroup>
              </DrawingBrush.Drawing>
            </DrawingBrush>
          </Button.Background>
        </Button>

</Window>

   
     


DrawingGroup and GeometryGroup

image_pdfimage_print


   
  

<Window x:Class="Workspace.DockExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Workspace" Width="640" Height="480">

    <Window.Resources>
    <DrawingGroup x:Key="myDrawing" >
      <GeometryDrawing>
        <GeometryDrawing.Geometry>
          <GeometryGroup>
            <LineGeometry StartPoint="0,0" EndPoint="1,1" />
            <LineGeometry StartPoint="0,1" EndPoint="1,0" />
          </GeometryGroup>
        </GeometryDrawing.Geometry>
        <GeometryDrawing.Pen>
          <Pen Thickness="0.2" Brush="#3366FF" />
        </GeometryDrawing.Pen>
      </GeometryDrawing>
      <GeometryDrawing Brush="Red"
       Geometry="M 0.4,0.4 L 0.5,0.2 0.6,0.4 0.5,0.5" />
    </DrawingGroup>
    </Window.Resources>
    <Rectangle Width="175" Height="90" Stroke="Black">
      <Rectangle.Fill>
        <DrawingBrush Drawing="{StaticResource myDrawing}" Viewport="0,0,0.5,0.5" TileMode="Tile">
          <DrawingBrush.RelativeTransform>
            <RotateTransform CenterX="0.5" CenterY="0.5" Angle="45" />
          </DrawingBrush.RelativeTransform>
        </DrawingBrush>
      </Rectangle.Fill>
    </Rectangle>

</Window>

   
    
     


Draw text to the background of a control by accessing the control's DrawingContext.

image_pdfimage_print


   
  


<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Draw Text to a Control&#039;s Background"
  Background="FloralWhite"
  Width="800" 
  Loaded="WindowLoaded">

  <StackPanel>
      <Label Name="myLabel" Width="200" Height="36" />
      <TextBox Name="myTextBox" MaxLength="25" >Sample Text</TextBox>
      <Button Name="myButton" Click="OnButtonClick" Width="160" Height="40" />
      <Canvas Name="myCanvas" Width ="760" Height="300"/>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1() : base()
        {
            InitializeComponent();
        }

        private void WindowLoaded(object sender, EventArgs e) 
        {
            myLabel.Background = new DrawingBrush(DrawMyText("My Custom Label"));
            myButton.Background = new DrawingBrush(DrawMyText("Display Text"));
        }
        private Drawing DrawMyText(string textString)
        {
            DrawingGroup drawingGroup = new DrawingGroup();
            using (DrawingContext drawingContext = drawingGroup.Open())
            {
                FormattedText formattedText = new FormattedText(
                    textString,
                    CultureInfo.GetCultureInfo("en-us"),
                    FlowDirection.LeftToRight,
                    new Typeface("Comic Sans MS Bold"),
                    48,
                    Brushes.Black 
                    );
                Geometry textGeometry = formattedText.BuildGeometry(new Point(20, 0));
                drawingContext.DrawRoundedRectangle(Brushes.PapayaWhip, null, new Rect(new Size(formattedText.Width + 50, formattedText.Height + 5)), 5.0, 5.0);
                drawingContext.DrawGeometry(Brushes.Gold, new Pen(Brushes.Maroon, 1.5), textGeometry);
                return drawingGroup;
            }
        }
        public void OnButtonClick(object sender, EventArgs e)
        {
            myCanvas.Background = new DrawingBrush(DrawMyText(myTextBox.Text));
        }
    }
}

   
    
     


Create buttons using DrawingImage and GeometryDrawing

image_pdfimage_print


   
 

<Window x:Class="Chapter05.DrawingImageExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="DrawingImage Example" Height="130" Width="320">
    <Button Name="DrawRectangle" Grid.Column="1"
      ToolTip="Draw Rectangle" Margin="5">
      <Image Width="80" Height="80">
        <Image.Source>
          <DrawingImage>
            <DrawingImage.Drawing>
              <GeometryDrawing Brush="LightGray">
                <GeometryDrawing.Geometry>
                  <RectangleGeometry Rect="0,20,100,60" />
                </GeometryDrawing.Geometry>
                <GeometryDrawing.Pen>
                  <Pen Brush="Gray" Thickness="3" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
            </DrawingImage.Drawing>
          </DrawingImage>
        </Image.Source>
      </Image>
    </Button>

</Window>