Aracılığıyla paylaş


Nasıl yapılır: Görüntü Öğesi Kullanma

Bu örnek, öğesini kullanarak Image bir uygulamaya görüntü eklemeyi gösterir.

Resim tanımlama

Aşağıdaki örnekte, 200 piksel genişliğinde bir görüntünün nasıl işlenme şekli gösterilmektedir. Bu Genişletilebilir Uygulama Biçimlendirme Dili (XAML) örneğinde, görüntüyü tanımlamak için hem öznitelik söz dizimi hem de özellik etiketi söz dizimi kullanılır. Öznitelik söz dizimi ve özellik söz dizimi hakkında daha fazla bilgi için bkz . Bağımlılık Özelliklerine Genel Bakış. , BitmapImage görüntünün kaynak verilerini tanımlamak için kullanılır ve özellik etiketi söz dizimi örneği için açıkça tanımlanır. Buna ek olarak, DecodePixelWidth öğesinin BitmapImage genişliği ile aynı genişliğe WidthImageayarlanır. Bu, görüntüyü işlerken en düşük bellek miktarının kullanıldığından emin olmak için yapılır.

Dekont

Genel olarak, işlenen görüntünün boyutunu belirtmek istiyorsanız, yalnızca Width veya Height değerini belirtin, ancak ikisini birden belirtmeyin. Yalnızca bir tane belirtirseniz görüntünün en boy oranı korunur. Aksi takdirde, görüntü beklenmedik şekilde esnetilmiş veya çarpıtılmış görünebilir. Görüntünün esnetme davranışını denetlemek için ve StretchDirection özelliklerini kullanınStretch.

Dekont

ya Heightda Width ile bir görüntünün boyutunu belirttiğinizde, ya DecodePixelHeight da aynı boyuta ayarlamanız DecodePixelWidth gerekir.

Stretch özelliği, görüntü kaynağının görüntü öğesini dolduracak şekilde nasıl genişletileceğini belirler. Daha fazla bilgi için numaralandırmaya Stretch bakın.

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

Görüntü işleme

Aşağıdaki örnekte, kod kullanarak 200 piksel genişliğinde bir görüntünün nasıl işlenme şekli gösterilmektedir.

Dekont

Özellikleri ayarlama BitmapImage işlemi bir BeginInit ve EndInit bloğu içinde yapılmalıdır.

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

Ayrıca bkz.