DrawingContext 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
描述使用绘图、推送和弹出命令的视觉内容。
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 该命令。 从 DrawingContext 控件 DrawingGroup 获取并显示 Image 该控件。
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
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));
}
// 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))
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
注解
使用 a DrawingContext 填充 Visual 或 Drawing 填充视觉对象内容。
DrawingContext虽然绘图方法与类型的绘图方法System.Drawing.Graphics类似,但它们的工作方式大相径庭:DrawingContext与保留模式图形系统一起使用,而该System.Drawing.Graphics类型与即时模式图形系统一起使用。 使用 DrawingContext 对象的绘图命令时,实际上会存储一组呈现指令(尽管确切的存储机制取决于图形系统稍后将使用的对象类型 DrawingContext);你不会实时绘制到屏幕。 有关 Windows Presentation Foundation (WPF) 图形系统的工作原理的详细信息,请参阅 WPF 图形呈现概述。
你从不直接实例化 DrawingContext;但是,可以从某些方法(如 DrawingGroup.Open 和 DrawingVisual.RenderOpen)获取绘图上下文。
属性
| 名称 | 说明 |
|---|---|
| Dispatcher |
获取与此DispatcherDispatcherObject关联的值。 (继承自 DispatcherObject) |
方法
显式接口实现
| 名称 | 说明 |
|---|---|
| IDisposable.Dispose() |
此成员支持 Windows Presentation Foundation (WPF) 基础结构,不应直接从代码使用。 |