此示例演示如何使用 Image 元素在应用程序中包括图像。
定义图像
以下示例演示如何呈现 200 像素宽的图像。 在此可扩展应用程序标记语言(XAML)示例中,属性语法和属性标记语法用于定义图像。 有关属性语法和属性语法的详细信息,请参阅 依赖属性概述。 A BitmapImage 用于定义图像的源数据,并为属性标记语法示例显式定义。 此外,DecodePixelWidthBitmapImage的宽度设置为与Image的Width宽度相同。 这样做是为了确保使用最小内存量来呈现图像。
注释
通常,如果要指定渲染图像的大小,请只指定 Width 或 Height,但不是两个都。 如果仅指定一个,则保留图像的纵横比。 否则,图像可能会意外地显示为拉伸或扭曲。 若要控制图像的拉伸行为,请使用 Stretch 和 StretchDirection 属性。
注释
指定Width或Height图像的大小时,应同时将DecodePixelWidth或DecodePixelHeight设定为相同的大小。
该 Stretch 属性确定如何拉伸图像源以填充图像元素。 有关详细信息,请参见 Stretch 枚举。
<!-- Simple image rendering. However, rendering an image this way may not
result in the best use of application memory. See markup below which
creates the same end result but using less memory. -->
<Image Width="200"
Source="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg"/>
<Image Width="200">
<Image.Source>
<!-- To save significant application memory, set the DecodePixelWidth or
DecodePixelHeight of the BitmapImage value of the image source to the desired
height and width of the rendered image. If you don't do this, the application will
cache the image as though it were rendered as its normal size rather than just
the size that is displayed. -->
<!-- Note: In order to preserve aspect ratio, only set either DecodePixelWidth
or DecodePixelHeight but not both. -->
<BitmapImage DecodePixelWidth="200"
UriSource="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg" />
</Image.Source>
</Image>
生成图像
以下示例演示如何使用代码呈现图像 200 像素宽。
注释
设置 BitmapImage 属性必须在一个 BeginInit 和 EndInit 块内完成。
// Create Image Element
Image myImage = new Image();
myImage.Width = 200;
// Create source
BitmapImage myBitmapImage = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");
// To save significant application memory, set the DecodePixelWidth or
// DecodePixelHeight of the BitmapImage value of the image source to the desired
// height or width of the rendered image. If you don't do this, the application will
// cache the image as though it were rendered as its normal size rather than just
// the size that is displayed.
// Note: In order to preserve aspect ratio, set DecodePixelWidth
// or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//set image source
myImage.Source = myBitmapImage;
' Create Image Element
Dim myImage As New Image()
myImage.Width = 200
' Create source
Dim myBitmapImage As New BitmapImage()
' BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit()
myBitmapImage.UriSource = New Uri("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg")
' To save significant application memory, set the DecodePixelWidth or
' DecodePixelHeight of the BitmapImage value of the image source to the desired
' height or width of the rendered image. If you don't do this, the application will
' cache the image as though it were rendered as its normal size rather than just
' the size that is displayed.
' Note: In order to preserve aspect ratio, set DecodePixelWidth
' or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200
myBitmapImage.EndInit()
'set image source
myImage.Source = myBitmapImage