如何:使用 Image 元素

此示例演示如何通过使用 Image 元素在应用程序中包含图像。

定义图像

下面的示例演示如何呈现宽为 200 像素的图像。 在此 Extensible Application Markup Language (XAML) 示例中,同时使用特性语法和属性标记语法来定义图像。 有关特性语法和属性语法的详细信息,请参阅依赖属性概述BitmapImage 用于定义图像的源数据,并且针对属性标记语法示例显式定义。 此外,BitmapImage 中的 DecodePixelWidth 的宽度设置为与 Image 中的 Width 的宽度相同。 这样做是为了确保呈现图像所使用的内存量最小。

注意

通常,如果你需要指定呈现图像的大小,只需指定 WidthHeight,但请勿同时指定两者。 如果仅指定一个,则会保持图像的纵横比。 否则,图像可能会出现意外的拉伸或扭曲。 若要控制图像的拉伸行为,请使用 StretchStretchDirection 属性。

注意

当使用 WidthHeight 指定图像大小时,还应该将 DecodePixelWidthDecodePixelHeight 设置为相同大小。

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 像素的图像。

注意

必须在 BeginInitEndInit 块中完成 BitmapImage 属性的设置。

// 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

另请参阅