更新:2007 年 11 月
本示例演示如何使用 Image 元素在应用程序中包括图像。
示例
下面的示例演示如何呈现 200 像素宽的图像。在此可扩展应用程序标记语言 (XAML) 示例中,同时使用了属性 (Attribute) 语法和属性 (Property) 标记语法来定义图像。有关属性 (attribute) 语法或属性 (property) 语法的更多信息,请参见依赖项属性概述。BitmapImage 用于定义图像的源数据,并且是为属性标记语法示例显式定义的。另外,BitmapImage 的 DecodePixelWidth 设置为与 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 then 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 像素宽的图像。
![]() |
---|
必须在 BeginInit 和 EndInit 块内执行 BitmapImage 属性的设置。 |
' 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 then 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
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 then 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;