Nasıl yapılır: Görüntü Öğesi Kullanma
Bu örnek, öğesini kullanarak bir uygulamaya görüntülerin nasıl ekleneceğini gösterir Image .
Örnek
Aşağıdaki örnek bir görüntünün 200 piksel genişliğinde nasıl işleneceğini gösterir. Bu Extensible Application Markup Language (XAML) örneğinde, görüntüyü tanımlamak için hem öznitelik sözdizimi hem de özellik etiketi sözdizimi kullanılır. Öznitelik sözdizimi ve özellik sözdizimi 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özdizimi örneği için açıkça tanımlanmıştır. Ayrıca, öğesinin ' ı DecodePixelWidthBitmapImage ile aynı genişliğe ayarlanır WidthImage . Bu, görüntüyü işlemek için en az bellek miktarının kullanıldığından emin olmak için yapılır.
Not
Genel olarak, işlenmiş bir görüntünün boyutunu belirtmek istiyorsanız, Width her ikisini de yalnızca veya ' ı belirtin Height . Yalnızca bir tane belirtirseniz, görüntünün en boy oranı korunur. Aksi takdirde, görüntü beklenmedik şekilde uzatılmış veya çarpıtılmış görünebilir. Görüntünün uzatma davranışını denetlemek için Stretch ve StretchDirection özelliklerini kullanın.
Not
Ya da ile bir görüntünün boyutunu belirttiğinizde, ya da WidthHeight aynı boyuta göre de ayarlamanız gerekir DecodePixelWidthDecodePixelHeight .
Özelliği, görüntü Stretch kaynağının görüntü öğesini doldurmak için nasıl uzatılacağını belirler. Daha fazla bilgi için bkz Stretch . sabit listesi.
<!-- 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>
Örnek
Aşağıdaki örnek, kod kullanarak bir görüntünün 200 piksel genelinde nasıl işleneceğini gösterir.
Not
Ayar BitmapImage özellikleri bir ve bloğu içinde yapılmalıdır BeginInitEndInit .
// 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;
' 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