DrawingContext 类

定义

使用绘图、推送和弹出命令描述可视内容。Describes visual content using draw, push, and pop commands.

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
实现

示例

下面的示例从 DrawingVisual 中检索 DrawingContext,并使用它来绘制矩形。The following example retrieves a DrawingContext from a DrawingVisual and uses it to draw a rectangle.

// 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

下一个示例演示 PushOpacityPushEffectPop 命令。The next example demonstrates the PushOpacity, PushEffect, and Pop commands. DrawingContext 是从 DrawingGroup 获取的,并使用 Image 控件进行显示。The DrawingContext is obtained from a DrawingGroup and displayed using an Image control.

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

注解

使用 DrawingContext 填充具有可视内容的 VisualDrawingUse a DrawingContext to populate a Visual or a Drawing with visual content.

尽管 DrawingContext 绘图方法看起来类似于 System.Drawing.Graphics 类型的绘制方法,但其工作方式非常不同: DrawingContext 用于保留模式图形系统,而 System.Drawing.Graphics 类型用于即时模式图形系统。Although the DrawingContext draw methods appear similar to the draw methods of the System.Drawing.Graphics type, they function very differently: DrawingContext is used with a retained mode graphics system, while the System.Drawing.Graphics type is used with an immediate mode graphics system. 使用 DrawingContext 对象的绘图命令时,实际上存储的是一组呈现说明(尽管具体的存储机制取决于提供 DrawingContext的对象类型),而该对象稍后将由图形系统使用;您不会实时绘制到屏幕上。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. 有关 Windows Presentation Foundation (WPF)图形系统的工作原理的详细信息,请参阅WPF 图形呈现概述For more information about how the Windows Presentation Foundation (WPF) graphics system works, see WPF Graphics Rendering Overview.

永远不会直接实例化 DrawingContext;不过,您可以从某些方法(如 DrawingGroup.OpenDrawingVisual.RenderOpen)获取一个绘图上下文。You never directly instantiate a DrawingContext; you can, however, acquire a drawing context from certain methods, such as DrawingGroup.Open and DrawingVisual.RenderOpen.

属性

Dispatcher

获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(继承自 DispatcherObject)

方法

CheckAccess()

确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)
Close()

关闭 DrawingContext 并刷新内容。Closes the DrawingContext and flushes the content. 此后,将不能修改 DrawingContextAfterward, the DrawingContext cannot be modified.

DisposeCore()

释放 DrawingContext 使用的所有资源。Releases all resources used by the DrawingContext.

DrawDrawing(Drawing)

绘制指定的 Drawing 对象。Draws the specified Drawing object.

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

使用指定的 BrushPen 绘制一个椭圆并应用指定的动画时钟。Draws an ellipse with the specified Brush and Pen and applies the specified animation clocks.

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

使用指定的 BrushPen 绘制一个椭圆。Draws an ellipse with the specified Brush and Pen.

DrawGeometry(Brush, Pen, Geometry)

使用指定的 GeometryBrush 绘制指定的 PenDraws the specified Geometry using the specified Brush and Pen.

DrawGlyphRun(Brush, GlyphRun)

绘制指定的文本。Draws the specified text.

DrawImage(ImageSource, Rect)

将图像绘制到由指定的 Rect 定义的区域中。Draws an image into the region defined by the specified Rect.

DrawImage(ImageSource, Rect, AnimationClock)

将图像绘制到由指定的 Rect 定义的区域中并应用指定的动画时钟。Draws an image into the region defined by the specified Rect and applies the specified animation clock.

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

使用指定的 Pen 在两个指定点之间绘制一条线,并应用指定的动画时钟。Draws a line between the specified points using the specified Pen and applies the specified animation clocks.

DrawLine(Pen, Point, Point)

使用指定的 Pen 在两个指定的点之间绘制一条线。Draws a line between the specified points using the specified Pen.

DrawRectangle(Brush, Pen, Rect)

使用指定的 BrushPen 绘制一个矩形。Draws a rectangle with the specified Brush and Pen. pen 和 brush 都可以为 nullThe pen and the brush can be null.

DrawRectangle(Brush, Pen, Rect, AnimationClock)

使用指定的 BrushPen 绘制一个矩形并应用指定的动画时钟。Draws a rectangle with the specified Brush and Pen and applies the specified animation clocks.

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

使用指定的 BrushPen 绘制一个圆角矩形并应用指定的动画时钟。Draws a rounded rectangle with the specified Brush and Pen and applies the specified animation clocks.

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

使用指定的 BrushPen 绘制一个圆角矩形。Draws a rounded rectangle with the specified Brush and Pen.

DrawText(FormattedText, Point)

在指定的位置绘制带格式文本。Draws formatted text at the specified location.

DrawVideo(MediaPlayer, Rect)

将视频绘制到指定区域内。Draws a video into the specified region.

DrawVideo(MediaPlayer, Rect, AnimationClock)

将视频绘制到指定区域内,并应用指定的动画时钟。Draws a video into the specified region and applies the specified animation clock.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Pop()

弹出推送到绘制上下文上的最后一个不透明蒙板、不透明度、剪辑、效果或转换操作。Pops the last opacity mask, opacity, clip, effect, or transform operation that was pushed onto the drawing context.

PushClip(Geometry)

将指定的剪辑区域推送到绘图上下文上。Pushes the specified clip region onto the drawing context.

PushEffect(BitmapEffect, BitmapEffectInput)

将指定的 BitmapEffect 推送到绘图上下文上。Pushes the specified BitmapEffect onto the drawing context.

PushGuidelineSet(GuidelineSet)

将指定的 GuidelineSet 推送到绘图上下文上。Pushes the specified GuidelineSet onto the drawing context.

PushOpacity(Double)

将指定的不透明度设置推送到绘图上下文上。Pushes the specified opacity setting onto the drawing context.

PushOpacity(Double, AnimationClock)

将指定的不透明度设置推送到绘图上下文上,并应用指定的动画时钟。Pushes the specified opacity setting onto the drawing context and applies the specified animation clock.

PushOpacityMask(Brush)

将指定的不透明蒙板推送到绘图上下文上。Pushes the specified opacity mask onto the drawing context.

PushTransform(Transform)

将指定的 Transform 推送到绘图上下文上。Pushes the specified Transform onto the drawing context.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
VerifyAccess()

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)
VerifyApiNonstructuralChange()

此成员支持 WPFWPF 基础结构,但不能在代码中直接使用。This member supports the WPFWPF infrastructure and is not intended to be used directly from your code.

显式接口实现

IDisposable.Dispose()

此成员支持 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基础结构,但不能在代码中直接使用。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

适用于

另请参阅