Stretch Property (Image)
Gets or sets a value that describes how an Image should be stretched to fill the destination rectangle.
<object Stretch="Stretch" .../>
value = object.Stretch
object.Stretch = value
This property is read/write. The default value is Uniform.
A value of Fill will cause your image to stretch to completely fill the output area. When the output area and the image have different aspect ratios, the image is distorted by this stretching. To make an Image preserve the aspect ratio of the image, set this property to Uniform (the default) or UniformToFill. Alternatively, you can leave Height and Width of the Image unset, which will display the image source at its true resolution, although this results in loss of control over the element sizing in XAML.
Setting the Height and Width creates a containing rectangular area that the image is displayed in. You can specify how the image fills this containing area using the Stretch property. The Stretch property accepts the following values, which the Stretch enumeration defines:
None: The image does not stretch to fill the output dimensions.
Uniform: This is the default value. The image is scaled to fit the output dimensions. However, the aspect ratio of the content is preserved.
UniformToFill: The image is scaled so that it completely fills the output area but preserves its original aspect ratio.
Fill: The video is scaled to fit the output dimensions. Because the content's height and width are scaled independently, the original aspect ratio of the image might not be preserved. That is, the image might be distorted in order to completely fill the output area.
The following image illustrates the different Stretch settings.
The example below shows how to display an Image that fills a 300 by 300 pixel output area but preserves its original aspect ratio by setting the Stretch property to UniformToFill.
<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300" x:Name="rootCanvas" Background="Gray"> <Image Source="myImage.jpg" Stretch="UniformToFill" Width="300" Height="300" /> </Canvas>
Note When only one constraint attribute is set (for example Height), the other attribute (in this case Width) is automatically calculated based upon the aspect ratio of the natural image. Because of this behavior, setting the Stretch property will not change the behavior unless it is set to None.