Procedura: Mantenere le proporzioni di un'immagine usata come sfondo
In questo esempio viene illustrato come utilizzare la proprietà Stretch di un ImageBrush per mantenere le proporzioni dell'immagine.
Per impostazione predefinita, quando si usa un ImageBrush per disegnare un'area, il contenuto si estende per riempire completamente l'area di output. Quando l'area di output e l'immagine hanno proporzioni diverse, l'immagine viene distorta da questa estensione.
Per far sì che un ImageBrush mantenga le proporzioni della sua immagine, impostare la proprietà Stretch su Uniform o UniformToFill.
Esempio
Nell'esempio seguente vengono utilizzati due oggetti ImageBrush per disegnare due rettangoli. Ogni rettangolo è 300 per 150 pixel e ognuno contiene un'immagine di 300 per 300 pixel. La proprietà Stretch del primo pennello è impostata su Uniforme la proprietà Stretch del secondo pennello è impostata su UniformToFill.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Microsoft.Samples.Graphics.UsingImageBrush
{
/// <summary>
/// Demonstrates different ImageBrush Stretch settings.
/// </summary>
public class StretchModes : Page
{
public StretchModes()
{
// Create an ImageBrush with its Stretch
// property set to Uniform. The image it
// contains will be expanded as much as possible
// to fill the output area while still
// preserving its aspect ratio.
ImageBrush uniformBrush = new ImageBrush();
uniformBrush.ImageSource =
new BitmapImage(new Uri("sampleImages\\square.jpg", UriKind.Relative));
uniformBrush.Stretch = Stretch.Uniform;
// Freeze the brush (make it unmodifiable) for performance benefits.
uniformBrush.Freeze();
// Create a rectangle and paint it with the ImageBrush.
Rectangle rectangle1 = new Rectangle();
rectangle1.Width = 300;
rectangle1.Height = 150;
rectangle1.Stroke = Brushes.MediumBlue;
rectangle1.StrokeThickness = 1.0;
rectangle1.Fill = uniformBrush;
// Create an ImageBrush with its Stretch
// property set to UniformToFill. The image it
// contains will be expanded to completely fill
// the rectangle, but its aspect ratio is preserved.
ImageBrush uniformToFillBrush = new ImageBrush();
uniformToFillBrush.ImageSource =
new BitmapImage(new Uri("sampleImages\\square.jpg", UriKind.Relative));
uniformToFillBrush.Stretch = Stretch.UniformToFill;
// Freeze the brush (make it unmodifiable) for performance benefits.
uniformToFillBrush.Freeze();
// Create a rectangle and paint it with the ImageBrush.
Rectangle rectangle2 = new Rectangle();
rectangle2.Width = 300;
rectangle2.Height = 150;
rectangle2.Stroke = Brushes.MediumBlue;
rectangle2.StrokeThickness = 1.0;
rectangle2.Margin = new Thickness(0, 10, 0, 0);
rectangle2.Fill = uniformToFillBrush;
StackPanel mainPanel = new StackPanel();
mainPanel.Children.Add(rectangle1);
mainPanel.Children.Add(rectangle2);
Content = mainPanel;
Background = Brushes.White;
Margin = new Thickness(20);
Title = "ImageBrush Stretch Modes";
}
}
}
La figura seguente mostra l'output del primo pennello, con un'impostazione Stretch di Uniform.
La figura seguente mostra l'output del secondo pennello, che ha un valore Stretch di UniformToFill.
Si noti che la proprietà Stretch si comporta in modo identico per gli altri oggetti TileBrush, ovvero per DrawingBrush e VisualBrush. Per altre informazioni su ImageBrush e sugli altri oggetti TileBrush, vedere Disegnare con immagini, disegni e oggetti visivi.
Si noti inoltre che, sebbene la proprietà Stretch sembri specificare il modo in cui il contenuto TileBrush si estende per adattarsi all'area di output, essa specifica effettivamente come il contenuto TileBrush si estende per riempire la sua piastrella di base. Per altre informazioni, vedere TileBrush.
Questo esempio di codice fa parte di un esempio più ampio fornito per la classe ImageBrush. Per l'esempio completo, vedere ImageBrush Sample.
Vedere anche
.NET Desktop feedback