您可以使用 ImageDrawing 绘制图像。 ImageDrawing 对象的 ImageSource 属性描述要绘制的图像,其 Rect 属性定义绘制图像的区域。
下面的示例将在一个矩形的 (75,75) 处绘制一个大小为 100 x 100 像素的图像。 下面的插图显示在示例中创建的 ImageDrawing。 添加了一条灰色边框,以显示ImageDrawing 的边界。
大小为 100 x 100 的 ImageDrawing
![在 (75,75) 处绘制的 100 x 100 ImageDrawing 在 (75,75) 处绘制的 100 x 100 ImageDrawing](http://i.msdn.microsoft.com/dynimg/IC149135.png)
后台代码:
// 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));
XAML代码:
<!-- The Rect property specifies that the image only fill a 100 by 100 rectangular area. -->
<ImageDrawingRect="75,75,100,100"ImageSource="sampleImages\kiwi.png"/>
总结:由以上的操作可以扩展成为类似于html元素中的background-image:position来定位小图的操作,这样可以大大提高了代码的重用,运行性能以及代码的扩展维护性。