Condividi tramite


Procedura: conservare le proporzioni di un'immagine utilizzata come sfondo

In questo esempio viene illustrato come utilizzare la Stretch proprietà di un ImageBrush oggetto per mantenere le proporzioni dell'immagine.

Per impostazione predefinita, quando si usa un ImageBrush oggetto per disegnare un'area, il relativo contenuto si estende per riempire completamente l'area di output. Quando l'area di output e l'immagine hanno proporzioni diverse, l'immagine risulta distorta dall'adattamento.

Per mantenere ImageBrush le proporzioni dell'immagine, impostare la Stretch proprietà su Uniform o UniformToFill.

Esempio

Nell'esempio seguente vengono utilizzati due ImageBrush oggetti per disegnare due rettangoli. Ogni rettangolo è di 300 x 150 pixel e contiene un'immagine di 300 x 300 pixel. La Stretch proprietà del primo pennello è impostata su Uniforme la Stretch proprietà del secondo pennello viene 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.

ImageBrush with Uniform stretching

La figura successiva mostra l'output del secondo pennello, con un'impostazione Stretch di UniformToFill.

ImageBrush with UniformToFill stretching

Si noti che la Stretch proprietà si comporta in modo identico per gli altri TileBrush oggetti, ovvero per DrawingBrush e VisualBrush. Per altre informazioni su ImageBrush e sugli altri TileBrush oggetti, vedere Disegnare con immagini, disegni e oggetti visivi.

Si noti anche che, anche se la Stretch proprietà sembra specificare il modo in cui il contenuto si adatta all'area TileBrush di output, specifica effettivamente il modo in cui il contenuto si estende per riempire il TileBrush riquadro di base. Per ulteriori informazioni, vedere TileBrush.

Questo esempio di codice fa parte di un esempio più ampio fornito per la ImageBrush classe . Per l'esempio completo, vedere ImageBrush Sample (Esempio ImageBrush).

Vedi anche