--- title: "How to: Preserve the Aspect Ratio of an Image Used as a Background" ms.date: "03/30/2017" helpviewer_keywords: - "aspect ratios of background images [WPF], preserving" - "brushes [WPF], preserving aspect ratios of background images" - "background images [WPF], preserving aspect ratios" ms.assetid: 28c39478-13d7-4011-80a3-8b9cc3e54478 --- # How to: Preserve the Aspect Ratio of an Image Used as a Background This example shows how to use the property of an in order to preserve the aspect ratio of the image. By default, when you use an to paint an area, its content stretches 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 preserve the aspect ratio of its image, set the property to or . ## Example The following example uses two objects to paint two rectangles. Each rectangle is 300 by 150 pixels and each contains a 300 by 300 pixel image. The property of the first brush is set to , and the property of the second brush is set to . [!code-csharp[UsingImageBrush_snip#ImageBrushStretchModesExampleWholePage](~/samples/snippets/csharp/VS_Snippets_Wpf/UsingImageBrush_snip/CSharp/StretchModes.cs#imagebrushstretchmodesexamplewholepage)] The following illustration shows the output of the first brush, which has a setting of . ![ImageBrush with Uniform stretching](./media/graphicsmm-imagebrushuniformstretch.jpg "graphicsmm_ImageBrushUniformStretch") The next illustration shows the output of the second brush, which has a setting of . ![ImageBrush with UniformToFill stretching](./media/graphicsmm-imagebrushuniformtofillstretch.jpg "graphicsmm_ImageBrushUniformToFillStretch") Note that the property behaves identically for the other objects, that is, for and . For more information about and the other objects, see [Painting with Images, Drawings, and Visuals](painting-with-images-drawings-and-visuals.md). Note also that, although the property appears to specify how the content stretches to fit its output area, it actually specifies how the content stretches to fill its base tile. For more information, see . This code example is part of a larger example that is provided for the class. For the complete sample, see [ImageBrush Sample](https://github.com/Microsoft/WPF-Samples/tree/master/Graphics/ImageBrush). ## See also - - [Painting with Images, Drawings, and Visuals](painting-with-images-drawings-and-visuals.md)