DrawingVisual.RenderOpen 方法

定义

打开用于呈现的 DrawingVisual 对象。 返回的 DrawingContext 值可用于呈现为 DrawingVisual

C#
public System.Windows.Media.DrawingContext RenderOpen();

返回

一个 DrawingContext 类型的值。

示例

在以下示例中,创建一个 DrawingVisual 对象,并返回该 DrawingContext 对象。

C#
// 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;
}

绘制到绘图上下文中的顺序非常重要 - 每个成功的绘图都绘制在其他绘图之上。 在以下示例中,首先绘制矩形,然后绘制文本。

C#
// 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();

注解

调用 Close 的 方法 DrawingContext时,当前绘图内容将替换为 DrawingVisual定义的任何以前的绘图内容。 这意味着无法将新绘图内容追加到现有绘图内容。

适用于

产品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅