Procedura: conservare le proporzioni di un'immagine utilizzata come sfondo
In questo esempio viene illustrato come utilizzare la proprietà Stretch di un oggetto ImageBrush per mantenere le proporzioni dell'immagine.
Per impostazione predefinita, quando si utilizza un oggetto ImageBrush per disegnare un'area, il contenuto si estende per riempire completamente l'area di output. Quando l'area di output e l'immagine hannoproporzionidifferenti, tale espansione distorce l'immagine.
Per fare in modo che un oggetto ImageBrush mantenga le proporzioni della relativa immagine, impostare la proprietà Stretch su Uniform o su UniformToFill.
Esempio
Nell'esempio riportato di seguito vengono utilizzati due oggetti ImageBrush per disegnare due rettangoli. Ogni rettangolo è di 300 x 150 pixel e contiene un immagine da 300 x 300 pixel. La proprietà Stretch del primo pennello è impostata su Uniform e la proprietà Stretch del secondo controllo è impostata su UniformToFill.
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media.Imaging
Imports System.Windows.Media
Imports System.Windows.Shapes
Namespace Microsoft.Samples.Graphics.UsingImageBrush
''' <summary>
''' Demonstrates different ImageBrush Stretch settings.
''' </summary>
Public Class StretchModes
Inherits Page
Public Sub New()
' 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.
Dim uniformBrush As 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.
Dim rectangle1 As New Rectangle()
With rectangle1
.Width = 300
.Height = 150
.Stroke = Brushes.MediumBlue
.StrokeThickness = 1.0
.Fill = uniformBrush
End With
' 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.
Dim uniformToFillBrush As 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.
Dim rectangle2 As New Rectangle()
With rectangle2
.Width = 300
.Height = 150
.Stroke = Brushes.MediumBlue
.StrokeThickness = 1.0
.Margin = New Thickness(0, 10, 0, 0)
.Fill = uniformToFillBrush
End With
Dim mainPanel As New StackPanel()
mainPanel.Children.Add(rectangle1)
mainPanel.Children.Add(rectangle2)
Content = mainPanel
Background = Brushes.White
Margin = New Thickness(20)
Title = "ImageBrush Stretch Modes"
End Sub
End Class
End Namespace
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";
}
}
}
<!-- Demonstrates different ImageBrush Stretch settings.-->
<Page
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:PresentationOptions="https://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="PresentationOptions"
Background="White" Margin="20"
Title="ImageBrush Stretch Modes">
<StackPanel>
<!-- The ImageBrush in this example has its
Stretch property set to Uniform. As a result,
the image it contains is expanded as much as possible
to fill the rectangle while
still preserving its aspect ratio.-->
<Rectangle
Width="300" Height="150"
Stroke="MediumBlue" StrokeThickness="1">
<Rectangle.Fill>
<ImageBrush
Stretch="Uniform" ImageSource="sampleImages\square.jpg"
PresentationOptions:Freeze="True" />
</Rectangle.Fill>
</Rectangle>
<!-- The ImageBrush in this example has its
Stretch property set to UniformToFill. As a result,
the image is expanded to completely fill
the rectangle, but its aspect ratio is preserved.-->
<Rectangle
Width="300" Height="150"
Stroke="MediumBlue" StrokeThickness="1"
Margin="0,10,0,0">
<Rectangle.Fill>
<ImageBrush
Stretch="UniformToFill" ImageSource="sampleImages\square.jpg"
PresentationOptions:Freeze="True" />
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</Page>
Nell'illustrazione seguente viene illustrato l'output del primo pennello, la cui proprietà Stretch è impostata su Uniform.
Nell'illustrazione successiva viene illustrato l'output del secondo pennello, la cui proprietà Stretch è impostata su UniformToFill.
Si noti che la proprietà Stretch ha lo stesso comportamento per gli altri oggetti TileBrush, ovvero per DrawingBrush e VisualBrush. Per ulteriori informazioni sugli oggetti ImageBrush e gli altri oggetti TileBrush, vedere Disegnare con oggetti Image, Drawing e Visual.
Si noti inoltre che, sebbene sembri che la proprietà Stretch specifichi in che modo il contenuto dell'oggetto TileBrush venga adattato all'area di output, in realtà specifica in che modo il contenuto dell'oggetto TileBrush venga adattato alla tessera di base. Per ulteriori informazioni, vedere TileBrush.
Questo esempio di codice fa parte di un esempio più esteso fornito per la classe ImageBrush. Per l'esempio completo, vedere Esempio ImageBrush (la pagina potrebbe essere in inglese).