Skip to main content
.NET Framework Class Library
DrawingContext Class

Describes visual content using draw, push, and pop commands.

Namespace:   System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)
Syntax
Public MustInherit Class DrawingContext _
	Inherits [%$TOPIC/ms653182_en-us_VS_110_2_0_0_0_0%] _
	Implements [%$TOPIC/ms653182_en-us_VS_110_2_0_0_0_1%]
public abstract class DrawingContext : [%$TOPIC/ms653182_en-us_VS_110_2_0_1_0_0%], 
	[%$TOPIC/ms653182_en-us_VS_110_2_0_1_0_1%]
public ref class DrawingContext abstract : public [%$TOPIC/ms653182_en-us_VS_110_2_0_2_0_0%], 
	[%$TOPIC/ms653182_en-us_VS_110_2_0_2_0_1%]
[<[%$TOPIC/ms653182_en-us_VS_110_2_0_3_0_0%]>]
type DrawingContext =  
    class 
        inherit [%$TOPIC/ms653182_en-us_VS_110_2_0_3_0_1%] 
        interface [%$TOPIC/ms653182_en-us_VS_110_2_0_3_0_2%] 
    end
public abstract class DrawingContext extends [%$TOPIC/ms653182_en-us_VS_110_2_0_4_0_0%] implements [%$TOPIC/ms653182_en-us_VS_110_2_0_4_0_1%]

The DrawingContext type exposes the following members.

Properties
  NameDescription
Public property DispatcherGets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Top
Methods
  NameDescription
Public method CheckAccessDetermines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Public method CloseCloses the DrawingContext and flushes the content. Afterward, the DrawingContext cannot be modified.
Protected method DisposeCoreReleases all resources used by the DrawingContext.
Public method DrawDrawingDraws the specified Drawing object.
Public method DrawEllipse(Brush, Pen, Point, Double, Double)Draws an ellipse with the specified Brush and Pen.
Public method DrawEllipse(Brush, Pen, Point, AnimationClock, Double, AnimationClock, Double, AnimationClock)Draws an ellipse with the specified Brush and Pen and applies the specified animation clocks.
Public method DrawGeometryDraws the specified Geometry using the specified Brush and Pen.
Public method DrawGlyphRunDraws the specified text.
Public method DrawImage(ImageSource, Rect)Draws an image into the region defined by the specified Rect.
Public method DrawImage(ImageSource, Rect, AnimationClock)Draws an image into the region defined by the specified Rect and applies the specified animation clock.
Public method DrawLine(Pen, Point, Point)Draws a line between the specified points using the specified Pen.
Public method DrawLine(Pen, Point, AnimationClock, Point, AnimationClock)Draws a line between the specified points using the specified Pen and applies the specified animation clocks.
Public method DrawRectangle(Brush, Pen, Rect)Draws a rectangle with the specified Brush and Pen. The pen and the brush can be .
Public method DrawRectangle(Brush, Pen, Rect, AnimationClock)Draws a rectangle with the specified Brush and Pen and applies the specified animation clocks.
Public method DrawRoundedRectangle(Brush, Pen, Rect, Double, Double)Draws a rounded rectangle with the specified Brush and Pen.
Public method DrawRoundedRectangle(Brush, Pen, Rect, AnimationClock, Double, AnimationClock, Double, AnimationClock)Draws a rounded rectangle with the specified Brush and Pen and applies the specified animation clocks.
Public method DrawTextDraws formatted text at the specified location.
Public method DrawVideo(MediaPlayer, Rect)Draws a video into the specified region.
Public method DrawVideo(MediaPlayer, Rect, AnimationClock)Draws a video into the specified region and applies the specified animation clock.
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public method PopPops the last opacity mask, opacity, clip, effect, or transform operation that was pushed onto the drawing context.
Public method PushClipPushes the specified clip region onto the drawing context.
Public method PushEffectObsolete. Pushes the specified BitmapEffect onto the drawing context.
Public method PushGuidelineSetPushes the specified GuidelineSet onto the drawing context.
Public method PushOpacity(Double)Pushes the specified opacity setting onto the drawing context.
Public method PushOpacity(Double, AnimationClock)Pushes the specified opacity setting onto the drawing context and applies the specified animation clock.
Public method PushOpacityMaskPushes the specified opacity mask onto the drawing context.
Public method PushTransformPushes the specified Transform onto the drawing context.
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Public method VerifyAccessEnforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Protected method VerifyApiNonstructuralChangeThis member supports the WPF infrastructure and is not intended to be used directly from your code. 
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method IDisposableDisposeThis member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.
Top
Remarks

Use a DrawingContext to populate a Visual or a Drawing with visual content.

Although the DrawingContext draw methods appear similar to the draw methods of the System.DrawingGraphics type, they function very differently: DrawingContext is used with a retained mode graphics system, while the System.DrawingGraphics type is used with an immediate mode graphics system. When you use a DrawingContext object's draw commands, you are actually storing a set of rendering instructions (although the exact storage mechanism depends on the type of object that supplies the DrawingContext) that will later be used by the graphics system; you are not drawing to the screen in real-time. For more information about how the Windows Presentation Foundation (WPF) graphics system works, see  WPF Graphics Rendering Overview.

You never directly instantiate a DrawingContext; you can, however, acquire a drawing context from certain methods, such as DrawingGroupOpen and DrawingVisualRenderOpen.

Examples

The following example retrieves a DrawingContext from a DrawingVisual and uses it to draw a rectangle.

		' Create a DrawingVisual that contains a rectangle.
		Private Function CreateDrawingVisualRectangle() As DrawingVisual
			Dim drawingVisual As New DrawingVisual()

			' Retrieve the DrawingContext in order to create new drawing content.
			Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()

			' Create a rectangle and draw it in the DrawingContext.
			Dim rect As New Rect(New Point(160, 100), New Size(320, 80))
			drawingContext.DrawRectangle(Brushes.LightBlue, CType(Nothing, Pen), rect)

			' Persist the drawing content.
			drawingContext.Close()

			Return drawingVisual
		End Function
// Create a DrawingVisual that contains a rectangle. 
private DrawingVisual CreateDrawingVisualRectangle()
{
    DrawingVisual drawingVisual = new DrawingVisual();

    // Retrieve the DrawingContext in order to create new drawing content.
    DrawingContext drawingContext = drawingVisual.RenderOpen();

    // Create a rectangle and draw it in the DrawingContext.
    Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80));
    drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);

    // Persist the drawing content.
    drawingContext.Close();

    return drawingVisual;
}

The next example demonstrates the PushOpacity, PushEffect, and Pop commands. The DrawingContext is obtained from a DrawingGroup and displayed using an Image control.

Imports System.Windows.Media.Animation
Imports System.Windows.Media.Effects

Namespace SDKSample


	Public Class PushEffectExample
		Inherits Page

		Public Sub New()
			Dim shapeOutlinePen As New Pen(Brushes.Black, 2)
			shapeOutlinePen.Freeze()

			' Create a DrawingGroup
			Dim dGroup As New DrawingGroup()

			' Obtain a DrawingContext from 
			' the DrawingGroup.
			Using dc As DrawingContext = dGroup.Open()
				' Draw a rectangle at full opacity.
				dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(0, 0, 25, 25))

				' Push an opacity change of 0.5. 
				' The opacity of each subsequent drawing will
				' will be multiplied by 0.5.
				dc.PushOpacity(0.5)

				' This rectangle is drawn at 50% opacity.
				dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(25, 25, 25, 25))

				' Blurs subsquent drawings. 
                dc.PushEffect(New BlurBitmapEffect(), Nothing)

				' This rectangle is blurred and drawn at 50% opacity (0.5 x 0.5). 
				dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(50, 50, 25, 25))

				' This rectangle is also blurred and drawn at 50% opacity.
				dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(75, 75, 25, 25))

				' Stop applying the blur to subsquent drawings.
				dc.Pop()

				' This rectangle is drawn at 50% opacity with no blur effect.
				dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(100, 100, 25, 25))
			End Using

			' Display the drawing using an image control.
			Dim theImage As New Image()
			Dim dImageSource As New DrawingImage(dGroup)
			theImage.Source = dImageSource

			Me.Content = theImage

		End Sub




	End Class

End Namespace
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Media.Effects;

namespace SDKSample
{


    public class PushEffectExample : Page
    {

        public PushEffectExample()
        {
            Pen shapeOutlinePen = new Pen(Brushes.Black, 2);
            shapeOutlinePen.Freeze();

            // Create a DrawingGroup
            DrawingGroup dGroup = new DrawingGroup();

            // Obtain a DrawingContext from  
            // the DrawingGroup. 
            using (DrawingContext dc = dGroup.Open())
            {
                // Draw a rectangle at full opacity.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(0, 0, 25, 25));

                // Push an opacity change of 0.5.  
                // The opacity of each subsequent drawing will 
                // will be multiplied by 0.5.
                dc.PushOpacity(0.5);

                // This rectangle is drawn at 50% opacity.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(25, 25, 25, 25));

                // Blurs subsquent drawings. 
                dc.PushEffect(new BlurBitmapEffect(), null);

                // This rectangle is blurred and drawn at 50% opacity (0.5 x 0.5). 
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(50, 50, 25, 25));

                // This rectangle is also blurred and drawn at 50% opacity.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(75, 75, 25, 25));

                // Stop applying the blur to subsquent drawings.
                dc.Pop();

                // This rectangle is drawn at 50% opacity with no blur effect.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(100, 100, 25, 25));
            }

            // Display the drawing using an image control.
            Image theImage = new Image();
            DrawingImage dImageSource = new DrawingImage(dGroup);
            theImage.Source = dImageSource;

            this.Content = theImage;

        }




    }

}
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.