DrawingContext 類別

定義

使用 draw、push 和 pop 命令來描述視覺內容。

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
實作

範例

下列範例會 DrawingContextDrawingVisual 擷取 ,並使用它繪製矩形。

// 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 命令。 會 DrawingContextDrawingGroup 取得 ,並使用 Image 控制項顯示。

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使用 填入 VisualDrawing 以視覺內容填入 。

DrawingContext雖然繪製方法看起來類似于類型的繪製方法 System.Drawing.Graphics ,但它們的運作方式非常不同: DrawingContext 與保留模式圖形系統搭配使用,而 System.Drawing.Graphics 類型則與立即模式圖形系統搭配使用。 當您使用 DrawingContext 物件的繪製命令時,實際上會儲存一組轉譯指令, (雖然確切儲存機制取決於提供 DrawingContext 圖形系統稍後將使用之) 的物件類型;您不會即時繪製到螢幕。 如需WINDOWS PRESENTATION FOUNDATION (WPF) 圖形系統運作方式的詳細資訊,請參閱WPF 圖形轉譯概觀。

您永遠不會直接具現化 DrawingContext ;不過,您可以從某些方法取得繪圖內容,例如 DrawingGroup.OpenDrawingVisual.RenderOpen

屬性

Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObject

(繼承來源 DispatcherObject)

方法

CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObject

(繼承來源 DispatcherObject)
Close()

關閉 DrawingContext,然後清除內容。 此後便就無法再修改 DrawingContext

DisposeCore()

釋放 DrawingContext 所使用的所有資源。

DrawDrawing(Drawing)

繪製指定的 Drawing 物件。

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

利用指定的 BrushPen 繪製橢圓形,然後套用指定的動畫時鐘。

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

利用指定的 BrushPen 繪製橢圓形。

DrawGeometry(Brush, Pen, Geometry)

使用指定的 BrushPen 繪製指定的 Geometry

DrawGlyphRun(Brush, GlyphRun)

繪製指定的文字。

DrawImage(ImageSource, Rect)

將影像繪製到由指定之 Rect 定義的區域。

DrawImage(ImageSource, Rect, AnimationClock)

將影像繪製至由指定之 Rect 定義的區域,然後套用指定的動畫時鐘。

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

使用指定的 Pen 在指定的點之間繪製線條,然後套用指定的動畫時鐘。

DrawLine(Pen, Point, Point)

使用指定的 Pen,在指定的點之間繪製線條。

DrawRectangle(Brush, Pen, Rect)

利用指定的 BrushPen 繪製矩形。 畫筆和筆刷可以是 null

DrawRectangle(Brush, Pen, Rect, AnimationClock)

利用指定的 BrushPen 繪製矩形,然後套用指定的動畫時鐘。

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

利用指定的 BrushPen 繪製圓角矩形,然後套用指定的動畫時鐘。

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

利用指定的 BrushPen 繪製圓角矩形。

DrawText(FormattedText, Point)

在指定的位置繪製格式化文字。

DrawVideo(MediaPlayer, Rect)

將視訊繪製至指定的區域。

DrawVideo(MediaPlayer, Rect, AnimationClock)

將視訊繪製至指定的區域,然後套用指定的動作時鐘。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Pop()

取出已推入至繪製內容的最後一個不透明遮罩、不透明度、裁剪、效果或轉換作業。

PushClip(Geometry)

將指定的裁剪區域推入至繪製內容。

PushEffect(BitmapEffect, BitmapEffectInput)
已淘汰.

將指定的 BitmapEffect 推入至繪圖內容。

PushGuidelineSet(GuidelineSet)

將指定的 GuidelineSet 推入至繪圖內容。

PushOpacity(Double)

將指定的透明度設定推入至繪圖內容。

PushOpacity(Double, AnimationClock)

將指定的不透明度設定推入至繪製內容,然後套用指定的動畫時鐘。

PushOpacityMask(Brush)

將指定的不透明遮罩推入至繪製內容。

PushTransform(Transform)

將指定的 Transform 推入至繪圖內容。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
VerifyAccess()

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。

(繼承來源 DispatcherObject)
VerifyApiNonstructuralChange()

此成員支援 WPF 基礎結構,不適合直接從您的程式碼使用。

明確介面實作

IDisposable.Dispose()

此成員支援Windows Presentation Foundation (WPF) 基礎結構,而且不適合直接從程式碼使用。

適用於

另請參閱