Drawing Objects Overview

This topic introduces Drawing objects and describes how to use them to efficiently draw shapes, bitmaps, text, and media. Utilize Drawing objects when y'all create clip art, paint with a DrawingBrush, or use Visual objects.

What Is a Cartoon Object

A Drawing object describes visible content, such as a shape, bitmap, video, or a line of text. Different types of drawings depict different types of content. The following is a list of the unlike types of drawing objects.

  • GeometryDrawing – Draws a shape.

  • ImageDrawing – Draws an image.

  • GlyphRunDrawing – Draws text.

  • VideoDrawing – Plays an audio or video file.

  • DrawingGroup – Draws other drawings. Utilize a drawing grouping to combine other drawings into a single composite cartoon.

Drawing objects are versatile; there are many ways you can utilise a Drawing object.

  • You can display it as an image by using a DrawingImage and an Image control.

  • You lot tin can use it with a DrawingBrush to paint an object, such as the Background of a Folio.

  • Yous can utilise it to describe the appearance of a DrawingVisual.

  • You can use it to enumerate the contents of a Visual.

WPF provides other types of objects that are capable of drawing shapes, bitmaps, text, and media. For example, you lot can too use Shape objects to draw shapes, and the MediaElement control provides another way to add together video to your application. And so when should yous use Cartoon objects? When you lot can cede framework level features to proceeds operation benefits or when you demand Freezable features. Because Cartoon objects lack support for Layout, input, and focus, they provide performance benefits that brand them ideal for describing backgrounds, clip art, and for depression-level drawing with Visual objects.

Because they are a type Freezable object, Drawing objects gain several special features, which include the following: they can be declared as resources, shared amidst multiple objects, fabricated read-but to ameliorate performance, cloned, and made thread-prophylactic. For more than information well-nigh the unlike features provided past Freezable objects, see the Freezable Objects Overview.

Draw a Shape

To draw a shape, you apply a GeometryDrawing. A geometry cartoon's Geometry belongings describes the shape to draw, its Brush holding describes how the interior of the shape should exist painted, and its Pen property describes how its outline should be drawn.

The post-obit case uses a GeometryDrawing to depict a shape. The shape is described by a GeometryGroup and 2 EllipseGeometry objects. The shape's interior is painted with a LinearGradientBrush and its outline is drawn with a Black Pen.

This example creates the following GeometryDrawing.

A GeometryDrawing of two ellipses
A GeometryDrawing

              // // Create the Geometry to depict. // GeometryGroup ellipses = new GeometryGroup(); ellipses.Children.Add(     new EllipseGeometry(new Signal(50,50), 45, 20)     ); ellipses.Children.Add(     new EllipseGeometry(new Point(fifty, 50), 20, 45)     );  // // Create a GeometryDrawing. // GeometryDrawing aGeometryDrawing = new GeometryDrawing(); aGeometryDrawing.Geometry = ellipses;  // Paint the drawing with a gradient. aGeometryDrawing.Brush =     new LinearGradientBrush(         Colors.Bluish,         Color.FromRgb(204,204,255),         new Bespeak(0,0),         new Bespeak(ane,1));  // Outline the drawing with a solid color. aGeometryDrawing.Pen = new Pen(Brushes.Blackness, 10);                          
              <GeometryDrawing>   <GeometryDrawing.Geometry>      <!-- Create a blended shape. -->     <GeometryGroup>       <EllipseGeometry Middle="50,fifty" RadiusX="45" RadiusY="twenty" />       <EllipseGeometry Heart="50,l" RadiusX="xx" RadiusY="45" />     </GeometryGroup>   </GeometryDrawing.Geometry>   <GeometryDrawing.Brush>      <!-- Paint the drawing with a gradient. -->     <LinearGradientBrush>       <GradientStop Starting time="0.0" Color="Bluish" />       <GradientStop Kickoff="1.0" Color="#CCCCFF" />     </LinearGradientBrush>   </GeometryDrawing.Brush>   <GeometryDrawing.Pen>      <!-- Outline the drawing with a solid colour. -->     <Pen Thickness="10" Brush="Black" />   </GeometryDrawing.Pen> </GeometryDrawing>                          

For the complete example, encounter Create a GeometryDrawing.

Other Geometry classes, such equally PathGeometry enable y'all to create more complex shapes by creating curves and arcs. For more information about Geometry objects, see the Geometry Overview.

For more information virtually other ways to describe shapes that don't use Drawing objects, see Shapes and Basic Cartoon in WPF Overview.

Describe an Epitome

To describe an image, you utilise an ImageDrawing. An ImageDrawing object's ImageSource property describes the paradigm to draw, and its Rect property defines the region where the image is drawn.

The following example draws an image into a rectangle located at (75,75) that is 100 by 100 pixel. The post-obit illustration shows the ImageDrawing created past the case. A gray edge was added to show the bounds of the ImageDrawing.

A 100 by 100 ImageDrawing drawn at (75,75)
A 100 by 100 ImageDrawing

              // Create a 100 by 100 image with an upper-left point of (75,75). ImageDrawing bigKiwi = new ImageDrawing(); bigKiwi.Rect = new Rect(75, 75, 100, 100); bigKiwi.ImageSource = new BitmapImage(     new Uri(@"sampleImages\kiwi.png", UriKind.Relative));                          
              <!-- The Rect property specifies that the epitome simply fill up a 100 by 100      rectangular area. --> <ImageDrawing Rect="75,75,100,100" ImageSource="sampleImages\kiwi.png"/>                          

For more information about images, run into the Imaging Overview.

Note

Although you can declare a VideoDrawing in Extensible Application Markup Language (XAML), you can only load and play its media using code. To play video in Extensible Awarding Markup Language (XAML), apply a MediaElement instead.

To play an sound or video file, you use a VideoDrawing and a MediaPlayer. There are two ways to load and play media. The offset is to employ a MediaPlayer and a VideoDrawing by themselves, and the 2d way is to create your own MediaTimeline to utilise with the MediaPlayer and VideoDrawing.

Note

When distributing media with your application, you cannot use a media file equally a project resource, like you would an image. In your project file, you must instead set the media type to Content and ready CopyToOutputDirectory to PreserveNewest or Always.

To play media without creating your own MediaTimeline, you perform the following steps.

  1. Create a MediaPlayer object.

                      MediaPlayer thespian = new MediaPlayer();                                  
  2. Use the Open method to load the media file.

                      player.Open(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));                                  
  3. Create a VideoDrawing.

                      VideoDrawing aVideoDrawing = new VideoDrawing();                                  
  4. Specify the size and location to draw the media by setting the Rect belongings of the VideoDrawing.

                      aVideoDrawing.Rect = new Rect(0, 0, 100, 100);                                  
  5. Ready the Thespian property of the VideoDrawing with the MediaPlayer you created.

                      aVideoDrawing.Player = player;                                  
  6. Use the Play method of the MediaPlayer to start playing the media.

                      // Play the video once. actor.Play();                                  

The following case uses a VideoDrawing and a MediaPlayer to play a video file once.

              // // Create a VideoDrawing. // MediaPlayer player = new MediaPlayer();  player.Open(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));  VideoDrawing aVideoDrawing = new VideoDrawing();  aVideoDrawing.Rect = new Rect(0, 0, 100, 100);  aVideoDrawing.Player = player;  // Play the video once. role player.Play();                          

To proceeds additional timing control over the media, use a MediaTimeline with the MediaPlayer and VideoDrawing objects. The MediaTimeline enables you to specify whether the video should echo. To employ a MediaTimeline with a VideoDrawing, yous perform the following steps:

  1. Declare the MediaTimeline and set its timing behaviors.

                      // Create a MediaTimeline. MediaTimeline mTimeline =     new MediaTimeline(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));  // Set the timeline to repeat. mTimeline.RepeatBehavior = RepeatBehavior.Forever;                                  
  2. Create a MediaClock from the MediaTimeline.

                      // Create a clock from the MediaTimeline. MediaClock mClock = mTimeline.CreateClock();                                  
  3. Create a MediaPlayer and apply the MediaClock to set its Clock property.

                      MediaPlayer repeatingVideoDrawingPlayer = new MediaPlayer(); repeatingVideoDrawingPlayer.Clock = mClock;                                  
  4. Create a VideoDrawing and assign the MediaPlayer to the Thespian property of the VideoDrawing.

                      VideoDrawing repeatingVideoDrawing = new VideoDrawing(); repeatingVideoDrawing.Rect = new Rect(150, 0, 100, 100); repeatingVideoDrawing.Player = repeatingVideoDrawingPlayer;                                  

The following example uses a MediaTimeline with a MediaPlayer and a VideoDrawing to play a video repeatedly.

              // // Create a VideoDrawing that repeats. //  // Create a MediaTimeline. MediaTimeline mTimeline =     new MediaTimeline(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));  // Ready the timeline to repeat. mTimeline.RepeatBehavior = RepeatBehavior.Forever;  // Create a clock from the MediaTimeline. MediaClock mClock = mTimeline.CreateClock();  MediaPlayer repeatingVideoDrawingPlayer = new MediaPlayer(); repeatingVideoDrawingPlayer.Clock = mClock;  VideoDrawing repeatingVideoDrawing = new VideoDrawing(); repeatingVideoDrawing.Rect = new Rect(150, 0, 100, 100); repeatingVideoDrawing.Player = repeatingVideoDrawingPlayer;                          

Notation that, when yous use a MediaTimeline, you use the interactive ClockController returned from the Controller property of the MediaClock to control media playback instead of the interactive methods of MediaPlayer.

Draw Text

To draw text, you use a GlyphRunDrawing and a GlyphRun. The following instance uses a GlyphRunDrawing to draw the text "Hullo World".

              GlyphRun theGlyphRun = new GlyphRun(     new GlyphTypeface(new Uri(@"C:\WINDOWS\Fonts\TIMES.TTF")),     0,     false,     13.333333333333334,     new ushort[]{43, 72, 79, 79, 82, iii, 58, 82, 85, 79, 71},     new Betoken(0, 12.29),     new double[]{         ix.62666666666667, 7.41333333333333, ii.96,         2.96, 7.41333333333333, three.70666666666667,         12.5866666666667, seven.41333333333333,         4.44, 2.96, 7.41333333333333},     null,     null,     null,     null,     cipher,     null      );  GlyphRunDrawing gDrawing = new GlyphRunDrawing(Brushes.Black, theGlyphRun);                          
              <GlyphRunDrawing ForegroundBrush="Black">   <GlyphRunDrawing.GlyphRun>     <GlyphRun        CaretStops="{10:Null}"        ClusterMap="{ten:Null}"        IsSideways="False"        GlyphOffsets="{x:Null}"        GlyphIndices="43 72 79 79 82 3 58 82 85 79 71"        BaselineOrigin="0,12.29"         FontRenderingEmSize="thirteen.333333333333334"        DeviceFontName="{x:Null}"        AdvanceWidths="9.62666666666667 7.41333333333333 2.96 ii.96 7.41333333333333 3.70666666666667 12.5866666666667 vii.41333333333333 4.44 2.96 7.41333333333333"        BidiLevel="0">       <GlyphRun.GlyphTypeface>         <GlyphTypeface FontUri="C:\WINDOWS\Fonts\TIMES.TTF" />       </GlyphRun.GlyphTypeface>     </GlyphRun>   </GlyphRunDrawing.GlyphRun> </GlyphRunDrawing>                          

A GlyphRun is a low-level object intended for use with fixed-format certificate presentation and impress scenarios. A simpler way to draw text to the screen is to use a Label or a TextBlock. For more information most GlyphRun, see the Introduction to the GlyphRun Object and Glyphs Element overview.

Composite Drawings

A DrawingGroup enables yous to combine multiple drawings into a single composite drawing. By using a DrawingGroup, you lot can combine shapes, images, and text into a single Drawing object.

The following example uses a DrawingGroup to combine two GeometryDrawing objects and an ImageDrawing object. This example produces the post-obit output.

A DrawingGroup with multiple drawings
A composite drawing

              // // Create three drawings. // GeometryDrawing ellipseDrawing =     new GeometryDrawing(         new SolidColorBrush(Color.FromArgb(102, 181, 243, xx)),         new Pen(Brushes.Blackness, 4),         new EllipseGeometry(new Point(50,fifty), 50, fifty)     );  ImageDrawing kiwiPictureDrawing =     new ImageDrawing(         new BitmapImage(new Uri(@"sampleImages\kiwi.png", UriKind.Relative)),         new Rect(50,fifty,100,100));  GeometryDrawing ellipseDrawing2 =     new GeometryDrawing(         new SolidColorBrush(Color.FromArgb(102,181,243,20)),         new Pen(Brushes.Black, 4),         new EllipseGeometry(new Bespeak(150, 150), fifty, 50)     );  // Create a DrawingGroup to incorporate the drawings. DrawingGroup aDrawingGroup = new DrawingGroup(); aDrawingGroup.Children.Add(ellipseDrawing); aDrawingGroup.Children.Add(kiwiPictureDrawing); aDrawingGroup.Children.Add(ellipseDrawing2);                          
              <DrawingGroup>    <GeometryDrawing Brush="#66B5F314">     <GeometryDrawing.Geometry>       <EllipseGeometry Centre="50,50" RadiusX="50"  RadiusY="50"/>     </GeometryDrawing.Geometry>     <GeometryDrawing.Pen>       <Pen Brush="Black" Thickness="4" />     </GeometryDrawing.Pen>   </GeometryDrawing>   <ImageDrawing ImageSource="sampleImages\kiwi.png" Rect="50,50,100,100"/>   <GeometryDrawing Castor="#66B5F314">     <GeometryDrawing.Geometry>       <EllipseGeometry Eye="150,150" RadiusX="fifty"  RadiusY="50"/>     </GeometryDrawing.Geometry>     <GeometryDrawing.Pen>       <Pen Castor="Black" Thickness="4" />     </GeometryDrawing.Pen>   </GeometryDrawing> </DrawingGroup>                          

A DrawingGroup also enables y'all to apply opacity masks, transforms, bitmap effects, and other operations to its contents. DrawingGroup operations are applied in the following order: OpacityMask, Opacity, BitmapEffect, ClipGeometry, GuidelineSet, and then Transform.

The following illustration shows the order in which DrawingGroup operations are practical.

DrawingGroup order of operations
Guild of DrawingGroup operations

The post-obit table describes the backdrop you can use to dispense a DrawingGroup object'due south contents.

Property Description Illustration
OpacityMask Alters the opacity of selected portions of the DrawingGroup contents. For an example, encounter How to: Control the Opacity of a Drawing. A DrawingGroup with an opacity mask
Opacity Uniformly changes the opacity of the DrawingGroup contents. Utilise this property to make a Drawing transparent or partially transparent. For an example, run into How to: Utilize an Opacity Mask to a Drawing. DrawingGroups with different opacity settings
BitmapEffect Applies a BitmapEffect to the DrawingGroup contents. For an example, see How to: Apply a BitmapEffect to a Drawing. DrawingGroup with a BlurBitmapEffect
ClipGeometry Clips the DrawingGroup contents to a region you depict using a Geometry. For an case, see How to: Clip a Drawing . DrawingGroup with a defined clip region
GuidelineSet Snaps device independent pixels to device pixels forth the specified guidelines. This belongings is useful for ensuring that finely detailed graphics render sharply on low-DPI displays. For an case, meet Apply a GuidelineSet to a Cartoon. A DrawingGroup with and without a GuidelineSet
Transform Transforms the DrawingGroup contents. For an example, see How to: Employ a Transform to a Cartoon. A rotated DrawingGroup

Brandish a Cartoon as an Image

To brandish a Drawing with an Image control, utilize a DrawingImage as the Image control's Source and ready the DrawingImage object's DrawingImage.Drawing holding to the drawing you want to display.

The following example uses a DrawingImage and an Image control to display a GeometryDrawing. This case produces the following output.

A GeometryDrawing of two ellipses
A DrawingImage

              using System; using System.Windows; using Arrangement.Windows.Controls; using Arrangement.Windows.Media; using Organization.Windows.Media.Blitheness; using Organization.Windows.Shapes;  namespace SDKSample {     public class DrawingImageExample : Page     {          public DrawingImageExample()         {              //             // Create the Geometry to draw.             //             GeometryGroup ellipses = new GeometryGroup();             ellipses.Children.Add(                 new EllipseGeometry(new Point(50,50), 45, 20)                 );             ellipses.Children.Add(                 new EllipseGeometry(new Point(50, 50), 20, 45)                 );              //             // Create a GeometryDrawing.             //             GeometryDrawing aGeometryDrawing = new GeometryDrawing();             aGeometryDrawing.Geometry = ellipses;              // Paint the drawing with a gradient.             aGeometryDrawing.Brush =                 new LinearGradientBrush(                     Colors.Bluish,                     Color.FromRgb(204,204,255),                     new Point(0,0),                     new Point(one,1));              // Outline the drawing with a solid color.             aGeometryDrawing.Pen = new Pen(Brushes.Black, 10);              //             // Utilise a DrawingImage and an Paradigm control             // to display the cartoon.             //             DrawingImage geometryImage = new DrawingImage(aGeometryDrawing);              // Freeze the DrawingImage for performance benefits.             geometryImage.Freeze();              Image anImage = new Image();             anImage.Source = geometryImage;             anImage.HorizontalAlignment = HorizontalAlignment.Left;              //             // Identify the image within a border and             // add it to the folio.             //             Edge exampleBorder = new Border();             exampleBorder.Child = anImage;             exampleBorder.BorderBrush = Brushes.Gray;             exampleBorder.BorderThickness = new Thickness(i);             exampleBorder.HorizontalAlignment = HorizontalAlignment.Left;             exampleBorder.VerticalAlignment = VerticalAlignment.Top;             exampleBorder.Margin = new Thickness(x);              this.Margin = new Thickness(twenty);             this.Background = Brushes.White;             this.Content = exampleBorder;         }     } }                          
              <Page    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   mc:Ignorable="PresentationOptions"   Background="White" Margin="20">    <Border BorderBrush="Gray" BorderThickness="1"      HorizontalAlignment="Left" VerticalAlignment="Top"     Margin="ten">      <!-- This paradigm uses a Drawing object for its source. -->     <Image>       <Epitome.Source>         <DrawingImage PresentationOptions:Freeze="True">           <DrawingImage.Drawing>             <GeometryDrawing>               <GeometryDrawing.Geometry>                 <GeometryGroup>                   <EllipseGeometry Center="50,50" RadiusX="45" RadiusY="xx" />                   <EllipseGeometry Center="50,50" RadiusX="20" RadiusY="45" />                 </GeometryGroup>               </GeometryDrawing.Geometry>               <GeometryDrawing.Brush>                 <LinearGradientBrush>                   <GradientStop Offset="0.0" Color="Blueish" />                   <GradientStop Offset="1.0" Colour="#CCCCFF" />                 </LinearGradientBrush>               </GeometryDrawing.Brush>               <GeometryDrawing.Pen>                 <Pen Thickness="10" Brush="Black" />               </GeometryDrawing.Pen>             </GeometryDrawing>           </DrawingImage.Drawing>         </DrawingImage>       </Image.Source>     </Image>   </Border>  </Page>                          

Pigment an Object with a Drawing

A DrawingBrush is a blazon of castor that paints an area with a drawing object. You can utilize it to pigment but almost any graphical object with a drawing. The Drawing holding of a DrawingBrush describes its Drawing. To render a Drawing with a DrawingBrush, add it to the brush using the brush'southward Cartoon property and apply the brush to pigment a graphical object, such as a control or panel.

The following examples uses a DrawingBrush to pigment the Fill of a Rectangle with a pattern created from a GeometryDrawing. This example produces the following output.

A tiled DrawingBrush
A GeometryDrawing used with a DrawingBrush

              using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Blitheness; using System.Windows.Shapes;  namespace SDKSample {     public class DrawingBrushExample : Page     {          public DrawingBrushExample()         {              //             // Create the Geometry to draw.             //             GeometryGroup ellipses = new GeometryGroup();             ellipses.Children.Add together(                 new EllipseGeometry(new Point(50,50), 45, xx)                 );             ellipses.Children.Add(                 new EllipseGeometry(new Point(50, 50), twenty, 45)                 );              //             // Create a GeometryDrawing.             //             GeometryDrawing aGeometryDrawing = new GeometryDrawing();             aGeometryDrawing.Geometry = ellipses;              // Pigment the drawing with a slope.             aGeometryDrawing.Brush =                 new LinearGradientBrush(                     Colors.Blue,                     Color.FromRgb(204,204,255),                     new Point(0,0),                     new Point(1,i));              // Outline the drawing with a solid color.             aGeometryDrawing.Pen = new Pen(Brushes.Black, 10);              DrawingBrush patternBrush = new DrawingBrush(aGeometryDrawing);             patternBrush.Viewport = new Rect(0, 0, 0.25, 0.25);             patternBrush.TileMode = TileMode.Tile;             patternBrush.Freeze();              //             // Create an object to pigment.             //             Rectangle paintedRectangle = new Rectangle();             paintedRectangle.Width = 100;             paintedRectangle.Height = 100;             paintedRectangle.Fill = patternBrush;              //             // Place the prototype inside a border and             // add it to the page.             //             Border exampleBorder = new Edge();             exampleBorder.Child = paintedRectangle;             exampleBorder.BorderBrush = Brushes.Grey;             exampleBorder.BorderThickness = new Thickness(ane);             exampleBorder.HorizontalAlignment = HorizontalAlignment.Left;             exampleBorder.VerticalAlignment = VerticalAlignment.Top;             exampleBorder.Margin = new Thickness(10);              this.Margin = new Thickness(twenty);             this.Background = Brushes.White;             this.Content = exampleBorder;         }     } }                          
              <Folio    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   mc:Ignorable="PresentationOptions"   Margin="20" Background="White">    <Border BorderBrush="Gray" BorderThickness="1"      HorizontalAlignment="Left" VerticalAlignment="Top"     Margin="ten">     <Rectangle Width="100" Height="100">       <Rectangle.Fill up>         <DrawingBrush PresentationOptions:Freeze="True"                       Viewport="0,0,0.25,0.25" TileMode="Tile">           <DrawingBrush.Cartoon>             <GeometryDrawing>               <GeometryDrawing.Geometry>                 <GeometryGroup>                   <EllipseGeometry Middle="fifty,50" RadiusX="45" RadiusY="20" />                   <EllipseGeometry Center="50,fifty" RadiusX="20" RadiusY="45" />                 </GeometryGroup>               </GeometryDrawing.Geometry>               <GeometryDrawing.Brush>                 <LinearGradientBrush>                   <GradientStop Offset="0.0" Color="Blue" />                   <GradientStop Starting time="i.0" Color="#CCCCFF" />                 </LinearGradientBrush>               </GeometryDrawing.Brush>               <GeometryDrawing.Pen>                 <Pen Thickness="10" Brush="Black" />               </GeometryDrawing.Pen>             </GeometryDrawing>           </DrawingBrush.Drawing>         </DrawingBrush>       </Rectangle.Fill>      </Rectangle>   </Edge>   </Page>                          

The DrawingBrush class provides a multifariousness of options for stretching and tiling its content. For more information nigh DrawingBrush, run into the Painting with Images, Drawings, and Visuals overview.

Return a Drawing with a Visual

A DrawingVisual is a type of visual object designed to render a drawing. Working directly at the visual layer is an choice for developers who want to build a highly customized graphical environment, and is not described in this overview. For more than information, see the Using DrawingVisual Objects overview.

DrawingContext Objects

The DrawingContext class enables you to populate a Visual or a Drawing with visual content. Many such lower-level graphics objects utilize a DrawingContext considering it describes graphical content very efficiently.

Although the DrawingContext draw methods appear like to the describe methods of the Organisation.Drawing.Graphics blazon, they are actually very different. DrawingContext is used with a retained mode graphics system, while the System.Cartoon.Graphics type is used with an immediate mode graphics arrangement. When you use a DrawingContext object's describe commands, y'all are actually storing a set of rendering instructions (although the exact storage machinery depends on the type of object that supplies the DrawingContext) that volition afterwards be used by the graphics system; y'all are not drawing to the screen in real-time. For more information about how the Windows Presentation Foundation (WPF) graphics system works, run into the WPF Graphics Rendering Overview.

You lot never directly instantiate a DrawingContext; you tin can, however, acquire a cartoon context from certain methods, such every bit DrawingGroup.Open and DrawingVisual.RenderOpen.

Enumerate the Contents of a Visual

In addition to their other uses, Drawing objects besides provide an object model for enumerating the contents of a Visual.

The following example uses the GetDrawing method to retrieve the DrawingGroup value of a Visual and enumerate it.

              public void RetrieveDrawing(Visual five) {     DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(five);     EnumDrawingGroup(drawingGroup); }  // Enumerate the drawings in the DrawingGroup. public void EnumDrawingGroup(DrawingGroup drawingGroup) {     DrawingCollection dc = drawingGroup.Children;      // Enumerate the drawings in the DrawingCollection.     foreach (Drawing cartoon in dc)     {         // If the drawing is a DrawingGroup, call the role recursively.         if (drawing is DrawingGroup group)         {             EnumDrawingGroup(group);         }         else if (drawing is GeometryDrawing)         {             // Perform action based on drawing type.         }         else if (drawing is ImageDrawing)         {             // Perform activity based on drawing type.         }         else if (cartoon is GlyphRunDrawing)         {             // Perform activeness based on drawing type.         }         else if (drawing is VideoDrawing)         {             // Perform action based on drawing blazon.         }     } }                          

See too

  • Cartoon
  • DrawingGroup
  • 2nd Graphics and Imaging
  • Painting with Images, Drawings, and Visuals
  • Geometry Overview
  • Shapes and Bones Drawing in WPF Overview
  • WPF Graphics Rendering Overview
  • Freezable Objects Overview
  • How-to Topics