DrawingContext DrawingContext DrawingContext DrawingContext Class

定義

使用 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
繼承
實作

範例

下列範例會擷取DrawingContextDrawingVisual並使用它來繪製矩形。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

下一個範例示範PushOpacityPushEffect,和Pop命令。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來填入VisualDrawing視覺內容。Use 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.

方法

CheckAccess() CheckAccess() CheckAccess() CheckAccess()

判斷呼叫的執行緒是否可以存取這個DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
Close() Close() Close() Close()

關閉 DrawingContext,然後清除內容。Closes the DrawingContext and flushes the content. 此後便就無法再修改 DrawingContextAfterward, the DrawingContext cannot be modified.

DisposeCore() DisposeCore() DisposeCore() DisposeCore()

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

DrawDrawing(Drawing) DrawDrawing(Drawing) DrawDrawing(Drawing) DrawDrawing(Drawing)

繪製指定的 Drawing 物件。Draws the specified Drawing object.

DrawEllipse(Brush, Pen, Point, AnimationClock, Double, AnimationClock, Double, AnimationClock) DrawEllipse(Brush, Pen, Point, AnimationClock, Double, AnimationClock, Double, AnimationClock) DrawEllipse(Brush, Pen, Point, AnimationClock, Double, AnimationClock, Double, AnimationClock) 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) DrawEllipse(Brush, Pen, Point, Double, Double) DrawEllipse(Brush, Pen, Point, Double, Double) DrawEllipse(Brush, Pen, Point, Double, Double)

利用指定的 BrushPen 繪製橢圓形。Draws an ellipse with the specified Brush and Pen.

DrawGeometry(Brush, Pen, Geometry) DrawGeometry(Brush, Pen, Geometry) DrawGeometry(Brush, Pen, Geometry) DrawGeometry(Brush, Pen, Geometry)

使用指定的 GeometryBrush 繪製指定的 PenDraws the specified Geometry using the specified Brush and Pen.

DrawGlyphRun(Brush, GlyphRun) DrawGlyphRun(Brush, GlyphRun) DrawGlyphRun(Brush, GlyphRun) DrawGlyphRun(Brush, GlyphRun)

繪製指定的文字。Draws the specified text.

DrawImage(ImageSource, Rect) DrawImage(ImageSource, Rect) DrawImage(ImageSource, Rect) DrawImage(ImageSource, Rect)

將影像繪製到由指定之 Rect 定義的區域。Draws an image into the region defined by the specified Rect.

DrawImage(ImageSource, Rect, AnimationClock) DrawImage(ImageSource, Rect, AnimationClock) DrawImage(ImageSource, Rect, AnimationClock) 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) DrawLine(Pen, Point, AnimationClock, Point, AnimationClock) DrawLine(Pen, Point, AnimationClock, Point, AnimationClock) 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) DrawLine(Pen, Point, Point) DrawLine(Pen, Point, Point) DrawLine(Pen, Point, Point)

使用指定的 Pen,在指定的點之間繪製線條。Draws a line between the specified points using the specified Pen.

DrawRectangle(Brush, Pen, Rect) DrawRectangle(Brush, Pen, Rect) DrawRectangle(Brush, Pen, Rect) 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) DrawRectangle(Brush, Pen, Rect, AnimationClock) DrawRectangle(Brush, Pen, Rect, AnimationClock) 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) DrawRoundedRectangle(Brush, Pen, Rect, AnimationClock, Double, AnimationClock, Double, AnimationClock) DrawRoundedRectangle(Brush, Pen, Rect, AnimationClock, Double, AnimationClock, Double, AnimationClock) 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) DrawRoundedRectangle(Brush, Pen, Rect, Double, Double) DrawRoundedRectangle(Brush, Pen, Rect, Double, Double) DrawRoundedRectangle(Brush, Pen, Rect, Double, Double)

利用指定的 BrushPen 繪製圓角矩形。Draws a rounded rectangle with the specified Brush and Pen.

DrawText(FormattedText, Point) DrawText(FormattedText, Point) DrawText(FormattedText, Point) DrawText(FormattedText, Point)

在指定的位置繪製格式化文字。Draws formatted text at the specified location.

DrawVideo(MediaPlayer, Rect) DrawVideo(MediaPlayer, Rect) DrawVideo(MediaPlayer, Rect) DrawVideo(MediaPlayer, Rect)

將視訊繪製至指定的區域。Draws a video into the specified region.

DrawVideo(MediaPlayer, Rect, AnimationClock) DrawVideo(MediaPlayer, Rect, AnimationClock) DrawVideo(MediaPlayer, Rect, AnimationClock) DrawVideo(MediaPlayer, Rect, AnimationClock)

將視訊繪製至指定的區域,然後套用指定的動作時鐘。Draws a video into the specified region and applies the specified animation clock.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Pop() Pop() Pop() Pop()

取出已推入至繪製內容的最後一個不透明遮罩、不透明度、裁剪、效果或轉換作業。Pops the last opacity mask, opacity, clip, effect, or transform operation that was pushed onto the drawing context.

PushClip(Geometry) PushClip(Geometry) PushClip(Geometry) PushClip(Geometry)

將指定的裁剪區域推入至繪製內容。Pushes the specified clip region onto the drawing context.

PushEffect(BitmapEffect, BitmapEffectInput) PushEffect(BitmapEffect, BitmapEffectInput) PushEffect(BitmapEffect, BitmapEffectInput) PushEffect(BitmapEffect, BitmapEffectInput)

將指定的 BitmapEffect 推入至繪圖內容。Pushes the specified BitmapEffect onto the drawing context.

PushGuidelineSet(GuidelineSet) PushGuidelineSet(GuidelineSet) PushGuidelineSet(GuidelineSet) PushGuidelineSet(GuidelineSet)

將指定的 GuidelineSet 推入至繪圖內容。Pushes the specified GuidelineSet onto the drawing context.

PushOpacity(Double) PushOpacity(Double) PushOpacity(Double) PushOpacity(Double)

將指定的透明度設定推入至繪圖內容。Pushes the specified opacity setting onto the drawing context.

PushOpacity(Double, AnimationClock) PushOpacity(Double, AnimationClock) PushOpacity(Double, AnimationClock) PushOpacity(Double, AnimationClock)

將指定的不透明度設定推入至繪製內容,然後套用指定的動畫時鐘。Pushes the specified opacity setting onto the drawing context and applies the specified animation clock.

PushOpacityMask(Brush) PushOpacityMask(Brush) PushOpacityMask(Brush) PushOpacityMask(Brush)

將指定的不透明遮罩推入至繪製內容。Pushes the specified opacity mask onto the drawing context.

PushTransform(Transform) PushTransform(Transform) PushTransform(Transform) PushTransform(Transform)

將指定的 Transform 推入至繪圖內容。Pushes the specified Transform onto the drawing context.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
VerifyAccess() VerifyAccess() VerifyAccess() VerifyAccess()

強制執行呼叫的執行緒可以存取這個DispatcherObjectEnforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
VerifyApiNonstructuralChange() VerifyApiNonstructuralChange() VerifyApiNonstructuralChange() VerifyApiNonstructuralChange()

這個成員支援 WPFWPF 基礎結構,但是您不可以從程式碼直接使用它。This member supports the WPFWPF infrastructure and is not intended to be used directly from your code.

明確介面實作

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() 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.

屬性

Dispatcher Dispatcher Dispatcher Dispatcher

取得DispatcherDispatcherObject相關聯。Gets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)

適用於

另請參閱