次の方法で共有


方法: Image 要素を使用する

この例では、 Image 要素を使用してアプリケーションにイメージを含める方法を示します。

イメージを定義する

次の例は、200 ピクセル幅の画像をレンダリングする方法を示しています。 この拡張アプリケーション マークアップ言語 (XAML) の例では、属性構文とプロパティ タグ構文の両方を使用してイメージを定義します。 属性の構文とプロパティの構文の詳細については、「依存関係プロパティの概要 を参照してください。 BitmapImageは、イメージのソース データを定義するために使用され、プロパティ タグ構文の例に対して明示的に定義されます。 また、DecodePixelWidthBitmapImageは、WidthImageと同じ幅に設定されます。 これは、イメージのレンダリングに使用されるメモリの最小量を確保するために行われます。

一般に、レンダリングされるイメージのサイズを指定する場合は、 Width または Height のみを指定しますが、両方は指定しないでください。 1 つだけを指定した場合、イメージの縦横比は保持されます。 そうしないと、イメージが予期せずストレッチまたは反って表示されることがあります。 イメージのストレッチ動作を制御するには、 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

こちらも参照ください