System.Windows.Media 命名空间


.NET Framework 类库
DrawingVisual 类

更新:2007 年 11 月

DrawingVisual 是一个可视对象,可用于在屏幕上呈现向量图形。内容由系统保存。

命名空间:  System.Windows.Media
程序集:  PresentationCore(在 PresentationCore.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation

语法

Visual Basic(声明)
Public Class DrawingVisual _
    Inherits ContainerVisual
Visual Basic (用法)
Dim instance As DrawingVisual
C#
public class DrawingVisual : ContainerVisual
Visual C++
public ref class DrawingVisual : public ContainerVisual
J#
public class DrawingVisual extends ContainerVisual
JScript
public class DrawingVisual extends ContainerVisual
XAML 对象元素用法
<DrawingVisual .../>
备注

DrawingVisual 是一个轻量绘图类,用于呈现形状、图像或文本。此类之所以被视为轻量类,是因为它不提供布局、输入、焦点或事件处理功能,从而能够改善其性能。因此,绘图是背景和剪贴画的理想选择。

为了使用 DrawingVisual 对象,需要为这些对象创建一个承载容器。该承载容器对象必须从 FrameworkElement 类中派生,该类可提供 DrawingVisual 类不支持的布局和事件处理支持。承载容器对象不显示任何可视属性,因为该对象的主要作用是包含子对象。有关更多信息,请参见使用 DrawingVisual 对象

有关演示如何使用承载容器创建 DrawingVisual 对象的完整示例,请参见使用 DrawingVisual 进行命中测试示例

示例

本示例演示如何从 Visual 创建位图。DrawingVisual 是使用 FormattedText 呈现的。Visual 然后呈现给 RenderTargetBitmap 以创建给定文本的位图。

Visual Basic
Dim myImage As New Image()
Dim [text] As New FormattedText("ABC", New CultureInfo("en-us"), System.Windows.FlowDirection.LeftToRight, New Typeface(Me.FontFamily, FontStyles.Normal, FontWeights.Normal, New FontStretch()), Me.FontSize, Me.Foreground)

Dim drawingVisual As New DrawingVisual()
Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
drawingContext.DrawText([text], New System.Windows.Point(2, 2))
drawingContext.Close()

Dim bmp As New RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32)
bmp.Render(drawingVisual)
myImage.Source = bmp
C#
Image myImage = new Image();
FormattedText text = new FormattedText("ABC",
        new CultureInfo("en-us"),
        FlowDirection.LeftToRight,
        new Typeface(this.FontFamily, FontStyles.Normal, FontWeights.Normal, new FontStretch()),
        this.FontSize,
        this.Foreground);

DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawText(text, new Point(2, 2));
drawingContext.Close();

RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
myImage.Source = bmp;
继承层次结构

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows.Media..::.ContainerVisual
          System.Windows.Media..::.DrawingVisual
线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
平台

Windows Vista

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

版本信息

.NET Framework

受以下版本支持:3.5、3.0
另请参见

参考

其他资源

标记 :


Page view tracker