如何:使用 Image 元素

本示例演示如何使用 Image 元素在应用程序中包括图像。

示例

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

注意注意

通常,如果您希望指定所呈现图像的大小,请要么指定 Width,要么指定 Height,而不要两者都指定。如果您仅指定其中之一,则会保持图像的纵横比。否则,图像可能会出现意外的拉伸或扭曲。若要控制图像的拉伸行为,请使用 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 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 像素宽的图像。

注意注意

必须在 BeginInitEndInit 块内执行 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;

请参见

概念

图像处理概述