DrawingContext 類別

定義

使用 draw、push 和 pop 命令來描述視覺內容。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.RenderOpenYou 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. 畫筆和筆刷可以是 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.

適用於

另請參閱