Drawing Class
Assembly: PresentationCore (in presentationcore.dll)
XML Namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation
[LocalizabilityAttribute(LocalizationCategory.None, Readability=Readability.Unreadable)] public abstract class Drawing : Animatable
/** @attribute LocalizabilityAttribute(LocalizationCategory.None, Readability=Readability.Unreadable) */ public abstract class Drawing extends Animatable
LocalizabilityAttribute(LocalizationCategory.None, Readability=Readability.Unreadable) public abstract class Drawing extends Animatable
This class is abstract; see Inheritance Hierarchy for derived non-abstract classes usable in XAML.
Drawing objects are light-weight objects that enable you to add geometric shapes, images, text, and media to an application. Drawing objects are considered light-weight because they do not provide support for layout, input, and focus. Because of their performance benefits, drawings are ideal for backgrounds and clip art. You also use drawings when programming at the Visual level.
Because they inherit from the Freezable class, Drawing objects provide additional features that make them useful for describing clip art and backgrounds: they can be declared as resources, shared among multiple objects, made read-only, and made thread-safe. For more information about the different features provided by Freezable objects, see the Freezable Objects Overview.
There are different types of Drawing objects for different types of content: GeometryDrawing, ImageDrawing, DrawingGroup, VideoDrawing, and GlyphRunDrawing.
Freezable Features
A Drawing is a type of Freezable object. For information about Freezable features, such as freezing and cloning, see the Freezable Objects Overview.
This example shows how to paint an area with a drawing. To paint an area with a drawing, you use a DrawingBrush and one or more Drawing objects. The following example uses a DrawingBrush to paint an object with a drawing of two ellipses.
<!-- Demonstrates the use of DrawingBrush. --> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="White"> <StackPanel Margin="20"> <Rectangle Width="150" Height="150" Stroke="Black" StrokeThickness="1"> <Rectangle.Fill> <DrawingBrush> <DrawingBrush.Drawing> <GeometryDrawing Brush="MediumBlue"> <GeometryDrawing.Geometry> <GeometryGroup> <EllipseGeometry RadiusX="20" RadiusY="45" Center="50,50" /> <EllipseGeometry RadiusX="45" RadiusY="20" Center="50,50" /> </GeometryGroup> </GeometryDrawing.Geometry> <GeometryDrawing.Pen> <Pen Thickness="10"> <Pen.Brush> <LinearGradientBrush> <GradientStop Offset="0.0" Color="Black" /> <GradientStop Offset="1.0" Color="Gray" /> </LinearGradientBrush> </Pen.Brush> </Pen> </GeometryDrawing.Pen> </GeometryDrawing> </DrawingBrush.Drawing> </DrawingBrush> </Rectangle.Fill> </Rectangle> </StackPanel> </Page>
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Microsoft.Samples.DrawingBrushExamples { /// <summary> /// Paints a Rectangle element with a DrawingBrush. /// </summary> public class DrawingBrushExample : Page { public DrawingBrushExample() { Background = Brushes.White; StackPanel mainPanel = new StackPanel(); // Create a drawing of two ellipses. GeometryDrawing aDrawing = new GeometryDrawing(); // Use geometries to describe two overlapping ellipses. EllipseGeometry ellipse1 = new EllipseGeometry(); ellipse1.RadiusX = 20; ellipse1.RadiusY = 45; ellipse1.Center = new Point(50, 50); EllipseGeometry ellipse2 = new EllipseGeometry(); ellipse2.RadiusX = 45; ellipse2.RadiusY = 20; ellipse2.Center = new Point(50, 50); GeometryGroup ellipses = new GeometryGroup(); ellipses.Children.Add(ellipse1); ellipses.Children.Add(ellipse2); // Add the geometry to the drawing. aDrawing.Geometry = ellipses; // Specify the drawing's fill. aDrawing.Brush = Brushes.Blue; // Specify the drawing's stroke. Pen stroke = new Pen(); stroke.Thickness = 10.0; stroke.Brush = new LinearGradientBrush( Colors.Black, Colors.Gray, new Point(0, 0), new Point(1, 1)); aDrawing.Pen = stroke; // Create a DrawingBrush DrawingBrush myDrawingBrush = new DrawingBrush(); myDrawingBrush.Drawing = aDrawing; // Create a Rectangle element. Rectangle aRectangle = new Rectangle(); aRectangle.Width = 150; aRectangle.Height = 150; aRectangle.Stroke = Brushes.Black; aRectangle.StrokeThickness = 1.0; // Use the DrawingBrush to paint the rectangle's // background. aRectangle.Fill = myDrawingBrush; mainPanel.Children.Add(aRectangle); this.Content = mainPanel; } } }
The following illustration shows the example's output.
(The center of the shape is white for reasons described in How to: Control the Fill of a Composite Shape.)
By setting a DrawingBrush object's Viewport and TileMode properties, you can create a repeating pattern. The following example paints an object with a pattern created from a drawing of two ellipses.
<!-- Demonstrates the use of DrawingBrush. --> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="White"> <StackPanel Margin="20"> <Rectangle Width="150" Height="150" Stroke="Black" StrokeThickness="1"> <Rectangle.Fill> <DrawingBrush Viewport="0,0,0.25,0.25" TileMode="Tile"> <DrawingBrush.Drawing> <GeometryDrawing Brush="MediumBlue"> <GeometryDrawing.Geometry> <GeometryGroup> <EllipseGeometry RadiusX="20" RadiusY="45" Center="50,50" /> <EllipseGeometry RadiusX="45" RadiusY="20" Center="50,50" /> </GeometryGroup> </GeometryDrawing.Geometry> <GeometryDrawing.Pen> <Pen Thickness="10"> <Pen.Brush> <LinearGradientBrush> <GradientStop Offset="0.0" Color="Black" /> <GradientStop Offset="1.0" Color="Gray" /> </LinearGradientBrush> </Pen.Brush> </Pen> </GeometryDrawing.Pen> </GeometryDrawing> </DrawingBrush.Drawing> </DrawingBrush> </Rectangle.Fill> </Rectangle> </StackPanel> </Page>
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Microsoft.Samples.DrawingBrushExamples { /// <summary> /// Paints a Rectangle element with a tiled DrawingBrush. /// </summary> public class TiledDrawingBrushExample : Page { public TiledDrawingBrushExample() { Background = Brushes.White; StackPanel mainPanel = new StackPanel(); // Create a drawing of two ellipses. GeometryDrawing aDrawing = new GeometryDrawing(); // Use geometries to describe two overlapping ellipses. EllipseGeometry ellipse1 = new EllipseGeometry(); ellipse1.RadiusX = 20; ellipse1.RadiusY = 45; ellipse1.Center = new Point(50, 50); EllipseGeometry ellipse2 = new EllipseGeometry(); ellipse2.RadiusX = 45; ellipse2.RadiusY = 20; ellipse2.Center = new Point(50, 50); GeometryGroup ellipses = new GeometryGroup(); ellipses.Children.Add(ellipse1); ellipses.Children.Add(ellipse2); // Add the geometry to the drawing. aDrawing.Geometry = ellipses; // Specify the drawing's fill. aDrawing.Brush = Brushes.Blue; // Specify the drawing's stroke. Pen stroke = new Pen(); stroke.Thickness = 10.0; stroke.Brush = new LinearGradientBrush( Colors.Black, Colors.Gray, new Point(0, 0), new Point(1, 1)); aDrawing.Pen = stroke; // Create a DrawingBrush DrawingBrush myDrawingBrush = new DrawingBrush(); myDrawingBrush.Drawing = aDrawing; // Set the DrawingBrush's Viewport and TileMode // properties so that it generates a pattern. myDrawingBrush.Viewport = new Rect(0, 0, 0.25, 0.25); myDrawingBrush.TileMode = TileMode.Tile; // Create a Rectangle element. Rectangle aRectangle = new Rectangle(); aRectangle.Width = 150; aRectangle.Height = 150; aRectangle.Stroke = Brushes.Black; aRectangle.StrokeThickness = 1.0; // Use the DrawingBrush to paint the rectangle's // background. aRectangle.Fill = myDrawingBrush; mainPanel.Children.Add(aRectangle); this.Content = mainPanel; } } }
The following illustration shows the tiled DrawingBrush output.
For more information about drawing brushes, see Painting with Images, Drawings, Visuals, and Patterns. For additional examples, see the DrawingBrush Sample. For more information about Drawing objects, see the Drawing Objects Overview.
More Code
| How to: Use a Drawing as an Image Source | This example shows how to use a Drawing as the Source for an Image control. To display a Drawing with an Image control, use a DrawingImage as the Image control's Source and set the DrawingImage object's DrawingImage.Drawing property to the drawing you want to display. |
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Freezable
System.Windows.Media.Animation.Animatable
System.Windows.Media.Drawing
System.Windows.Media.DrawingGroup
System.Windows.Media.GeometryDrawing
System.Windows.Media.GlyphRunDrawing
System.Windows.Media.ImageDrawing
System.Windows.Media.VideoDrawing
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Reference
Drawing MembersSystem.Windows.Media Namespace
ImageDrawing
GeometryDrawing
GlyphRunDrawing
DrawingGroup
VideoDrawing
DrawingBrush
ImageDrawing
Other Resources
Drawing Objects OverviewDrawings How-to Topics
Painting with Images, Drawings, Visuals, and Patterns
DrawingBrush Sample
Visual Layer Programming