DrawingContext 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用 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 從 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 、 PushEffect 和 Pop 命令。 會 DrawingContext 從 DrawingGroup 取得 ,並使用 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使用 填入 Visual 或 Drawing 以視覺內容填入 。
DrawingContext雖然繪製方法看起來類似于類型的繪製方法 System.Drawing.Graphics ,但它們的運作方式非常不同: DrawingContext 與保留模式圖形系統搭配使用,而 System.Drawing.Graphics 類型則與立即模式圖形系統搭配使用。 當您使用 DrawingContext 物件的繪製命令時,實際上會儲存一組轉譯指令, (雖然確切儲存機制取決於提供 DrawingContext 圖形系統稍後將使用之) 的物件類型;您不會即時繪製到螢幕。 如需WINDOWS PRESENTATION FOUNDATION (WPF) 圖形系統運作方式的詳細資訊,請參閱WPF 圖形轉譯概觀。
您永遠不會直接具現化 DrawingContext ;不過,您可以從某些方法取得繪圖內容,例如 DrawingGroup.Open 和 DrawingVisual.RenderOpen 。
屬性
Dispatcher |
取得與這個 Dispatcher 關聯的 DispatcherObject。 (繼承來源 DispatcherObject) |
方法
明確介面實作
IDisposable.Dispose() |
此成員支援Windows Presentation Foundation (WPF) 基礎結構,而且不適合直接從程式碼使用。 |