DrawingContext 类

定义

描述使用绘图、推送和弹出命令的视觉内容。

public ref class DrawingContext abstract : System::Windows::Threading::DispatcherObject, IDisposable
public abstract class DrawingContext : System.Windows.Threading.DispatcherObject, IDisposable
type DrawingContext = class
    inherit DispatcherObject
    interface IDisposable
Public MustInherit Class DrawingContext
Inherits DispatcherObject
Implements IDisposable
继承
DrawingContext
实现

示例

以下示例从 DrawingContext 中检索 DrawingVisual 并将其用于绘制矩形。

// 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;
}
' 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

下一个示例演示了 PushOpacity 该命令。 从 DrawingContext 控件 DrawingGroup 获取并显示 Image 该控件。

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
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));
            }

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

            this.Content = theImage;
        }
    }
}
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))
            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

注解

使用 a DrawingContext 填充 VisualDrawing 填充视觉对象内容。

DrawingContext虽然绘图方法与类型的绘图方法System.Drawing.Graphics类似,但它们的工作方式大相径庭:DrawingContext与保留模式图形系统一起使用,而该System.Drawing.Graphics类型与即时模式图形系统一起使用。 使用 DrawingContext 对象的绘图命令时,实际上会存储一组呈现指令(尽管确切的存储机制取决于图形系统稍后将使用的对象类型 DrawingContext);你不会实时绘制到屏幕。 有关 Windows Presentation Foundation (WPF) 图形系统的工作原理的详细信息,请参阅 WPF 图形呈现概述

你从不直接实例化 DrawingContext;但是,可以从某些方法(如 DrawingGroup.OpenDrawingVisual.RenderOpen)获取绘图上下文。

属性

名称 说明
Dispatcher

获取与此DispatcherDispatcherObject关联的值。

(继承自 DispatcherObject)

方法

名称 说明
CheckAccess()

确定调用线程是否有权访问此 DispatcherObject权限。

(继承自 DispatcherObject)
Close()

DrawingContext关闭并刷新内容。 之后, DrawingContext 无法修改。

DisposeCore()

释放该 DrawingContext命令使用的所有资源。

DrawDrawing(Drawing)

绘制指定的 Drawing 对象。

DrawEllipse(Brush, Pen, Point, AnimationClock, Double, AnimationClock, Double, AnimationClock)

使用指定的 Brush 椭圆绘制并 Pen 应用指定的动画时钟。

DrawEllipse(Brush, Pen, Point, Double, Double)

绘制具有指定 BrushPen.

DrawGeometry(Brush, Pen, Geometry)

使用指定的GeometryBrushPen

DrawGlyphRun(Brush, GlyphRun)

绘制指定的文本。

DrawImage(ImageSource, Rect, AnimationClock)

将图像绘制到指定 Rect 定义的区域中,并应用指定的动画时钟。

DrawImage(ImageSource, Rect)

将图像绘制到由指定 Rect区域定义的区域中。

DrawLine(Pen, Point, AnimationClock, Point, AnimationClock)

使用指定的点在指定 Pen 点之间绘制线条,并应用指定的动画时钟。

DrawLine(Pen, Point, Point)

使用指定的点在指定 Pen点之间绘制一条线。

DrawRectangle(Brush, Pen, Rect, AnimationClock)

使用指定的 Brush 矩形绘制并 Pen 应用指定的动画时钟。

DrawRectangle(Brush, Pen, Rect)

绘制具有指定 BrushPen. 笔和画笔可以是 null

DrawRoundedRectangle(Brush, Pen, Rect, AnimationClock, Double, AnimationClock, Double, AnimationClock)

使用指定的 Brush 矩形绘制圆角矩形,并 Pen 应用指定的动画时钟。

DrawRoundedRectangle(Brush, Pen, Rect, Double, Double)

绘制具有指定 BrushPen.

DrawText(FormattedText, Point)

在指定位置绘制带格式的文本。

DrawVideo(MediaPlayer, Rect, AnimationClock)

将视频绘制到指定区域并应用指定的动画时钟。

DrawVideo(MediaPlayer, Rect)

将视频绘制到指定区域。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Pop()

弹出推送到绘图上下文的最后一个不透明度掩码、不透明度、剪辑、效果或转换操作。

PushClip(Geometry)

将指定的剪辑区域推送到绘图上下文。

PushEffect(BitmapEffect, BitmapEffectInput)
已过时.

将指定的 BitmapEffect 内容推送到绘图上下文中。

PushGuidelineSet(GuidelineSet)

将指定的 GuidelineSet 内容推送到绘图上下文中。

PushOpacity(Double, AnimationClock)

将指定的不透明度设置推送到绘图上下文,并应用指定的动画时钟。

PushOpacity(Double)

将指定的不透明度设置推送到绘图上下文。

PushOpacityMask(Brush)

将指定的不透明度掩码推送到绘图上下文。

PushTransform(Transform)

将指定的 Transform 内容推送到绘图上下文中。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
VerifyAccess()

强制调用线程有权访问此 DispatcherObject权限。

(继承自 DispatcherObject)
VerifyApiNonstructuralChange()

此成员支持 WPF 基础结构,不打算直接从代码使用。

显式接口实现

名称 说明
IDisposable.Dispose()

此成员支持 Windows Presentation Foundation (WPF) 基础结构,不应直接从代码使用。

适用于

另请参阅