更新:2007 年 11 月
命名空间:
System.Windows.Media 程序集:
PresentationCore(在 PresentationCore.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation
Public NotInheritable Class ImageDrawing _
Inherits Drawing
Dim instance As ImageDrawing
public sealed class ImageDrawing : Drawing
public ref class ImageDrawing sealed : public Drawing
public final class ImageDrawing extends Drawing
public final class ImageDrawing extends Drawing
性能说明:与 Image 相比,DrawingImage 提供的呈现图像功能要少一些,但 DrawingImage 提供的性能具有一定的优势,是使用 Visual 对象对背景、剪贴画进行低级别绘制的理想选择。有关更多信息,请参见Drawing 对象概述。
ImageDrawing 是一种 Freezable 对象,因此可以将它冻结以提高性能。有关 Freezable 功能(例如冻结和克隆)的信息,请参见 Freezable 对象概述。
本示例演示如何使用 ImageDrawing 来绘制图像。使用 ImageDrawing,可以借助于 DrawingBrush、DrawingImage 或 Visual 来显示 ImageSource。若要绘制图像,请创建一个 ImageDrawing 并设置它的 ImageDrawing..::.ImageSource 和 ImageDrawing..::.Rect 属性。ImageDrawing..::.ImageSource 属性指定要绘制的图像,ImageDrawing..::.Rect 属性指定每个图像的位置和大小。
下面的示例使用四个 ImageDrawing 对象创建一个复合绘图。该示例生成下面的图像:
四个 ImageDrawing 对象
.jpg)
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
namespace SDKSample
{
public class ImageDrawingExample : Page
{
public ImageDrawingExample()
{
// Create a DrawingGroup to combine the ImageDrawing objects.
DrawingGroup imageDrawings = new DrawingGroup();
// Create a 100 by 100 image with an upper-left point of (75,75).
ImageDrawing bigKiwi = new ImageDrawing();
bigKiwi.Rect = new Rect(75, 75, 100, 100);
bigKiwi.ImageSource = new BitmapImage(
new Uri(@"sampleImages\kiwi.png", UriKind.Relative));
imageDrawings.Children.Add(bigKiwi);
// Create a 25 by 25 image with an upper-left point of (0,150).
ImageDrawing smallKiwi1 = new ImageDrawing();
smallKiwi1.Rect = new Rect(0, 150, 25, 25);
smallKiwi1.ImageSource = new BitmapImage(new Uri(@"sampleImages\kiwi.png", UriKind.Relative));
imageDrawings.Children.Add(smallKiwi1);
// Create a 25 by 25 image with an upper-left point of (150,0).
ImageDrawing smallKiwi2 = new ImageDrawing();
smallKiwi2.Rect = new Rect(150, 0, 25, 25);
smallKiwi2.ImageSource = new BitmapImage(new Uri(@"sampleImages\kiwi.png", UriKind.Relative));
imageDrawings.Children.Add(smallKiwi2);
// Create a 75 by 75 image with an upper-left point of (0,0).
ImageDrawing wholeKiwi = new ImageDrawing();
wholeKiwi.Rect = new Rect(0, 0, 75, 75);
wholeKiwi.ImageSource = new BitmapImage(new Uri(@"sampleImages\wholekiwi.png", UriKind.Relative));
imageDrawings.Children.Add(wholeKiwi);
//
// Use a DrawingImage and an Image control to
// display the drawings.
//
DrawingImage drawingImageSource = new DrawingImage(imageDrawings);
// Freeze the DrawingImage for performance benefits.
drawingImageSource.Freeze();
Image imageControl = new Image();
imageControl.Stretch = Stretch.None;
imageControl.Source = drawingImageSource;
// Create a border to contain the Image control.
Border imageBorder = new Border();
imageBorder.BorderBrush = Brushes.Gray;
imageBorder.BorderThickness = new Thickness(1);
imageBorder.HorizontalAlignment = HorizontalAlignment.Left;
imageBorder.VerticalAlignment = VerticalAlignment.Top;
imageBorder.Margin = new Thickness(20);
imageBorder.Child = imageControl;
this.Background = Brushes.White;
this.Margin = new Thickness(20);
this.Content = imageBorder;
}
}
}
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="PresentationOptions"
Background="White" Margin="20">
<Border BorderBrush="Gray" BorderThickness="1"
HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="20">
<Image Stretch="None">
<Image.Source>
<DrawingImage PresentationOptions:Freeze="True">
<DrawingImage.Drawing>
<DrawingGroup>
<!-- The Rect property specifies that the image only fill a 100 by 100
rectangular area. -->
<ImageDrawing Rect="75,75,100,100" ImageSource="sampleImages\kiwi.png"/>
<!-- This image is set to fill a 25 by 25 rectangular area. -->
<ImageDrawing Rect="0,150,25,25" ImageSource="sampleImages\kiwi.png"/>
<!-- This image is set to fill a 25 by 25 rectangular area. -->
<ImageDrawing Rect="150,0,25,25" ImageSource="sampleImages\kiwi.png"/>
<!-- This image is set to fill a 75 by 75 rectangular area. -->
<ImageDrawing Rect="0,0,75,75" ImageSource="sampleImages\wholekiwi.png"/>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
</Border>
</Page>
有关演示在不使用 ImageDrawing 的情况下显示图像的简单方法的示例,请参见如何:使用 Image 元素。
System..::.Object
System.Windows.Threading..::.DispatcherObject
System.Windows..::.DependencyObject
System.Windows..::.Freezable
System.Windows.Media.Animation..::.Animatable
System.Windows.Media..::.Drawing
System.Windows.Media..::.ImageDrawing
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0
参考
其他资源