Export (0) Print
Expand All

DrawingVisual.RenderOpen Method

Opens the DrawingVisual object for rendering. The returned DrawingContext value can be used to render into the DrawingVisual.

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)

public DrawingContext RenderOpen()
You cannot use methods in XAML.

Return Value

Type: System.Windows.Media.DrawingContext
A value of type DrawingContext.

When you call the Close method of the DrawingContext, the current drawing content replaces any previous drawing content defined for the DrawingVisual. This means that there is no way to append new drawing content to existing drawing content.

In the following example, a DrawingVisual object is created, and its DrawingContext returned.

// 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 order in which you draw into the drawing context is important—each succeeding drawing is drawn on top of the others. In the following example, the rectangle is drawn first, then the text.

// Retrieve the DrawingContext in order to draw into the visual object.
DrawingContext drawingContext = drawingVisual.RenderOpen();

// Draw a rectangle into the DrawingContext.
Rect rect = new Rect(new Point(160, 100), new Size(320, 80));
drawingContext.DrawRectangle(Brushes.LightBlue, (Pen)null, rect);

// Draw a formatted text string into the DrawingContext.
drawingContext.DrawText(
   new FormattedText("Hello, world",
      CultureInfo.GetCultureInfo("en-us"),
      FlowDirection.LeftToRight,
      new Typeface("Verdana"),
      36, Brushes.Black),
      new Point(200, 116));

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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

ADD
Show:
© 2015 Microsoft