共用方式為


HOW TO:保留當做背景之影像的外觀比例

更新:2007 年 11 月

本範例示範如何使用 ImageBrushStretch 屬性保留影像的外觀比例 (Aspect Ratio)。

根據預設,當您使用 ImageBrush 繪製區域時,它的內容會延伸填滿整個輸出區域。當輸出區域和影像的外觀比例不同時,影像會因延伸而扭曲。

若要 ImageBrush 保留影像的外觀比例,請將 Stretch 屬性設為 UniformUniformToFill

範例

下列範例使用兩個 ImageBrush 物件繪製兩個矩形。每個矩形都是 300 x 150 像素,而且各包含一個 300 x 300 像素的影像。第一個筆刷的 Stretch 屬性設為 Uniform,而第二個筆刷的 Stretch 屬性設為 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";


        }
    }
}
<!-- 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>

下圖顯示第一個筆刷的輸出,其中將 Stretch 設為 Uniform

Stretch 屬性設定為 Uniform 的 ImageBrush

下圖顯示第二個筆刷的輸出,其中將 Stretch 設為 UniformToFill

Stretch 屬性設定為 UniformToFill 的 ImageBrush

請注意,Stretch 屬性對其他 TileBrush 物件 (即 DrawingBrushVisualBrush) 的作用完全相同。如需 ImageBrush 和其他 TileBrush 物件的詳細資訊,請參閱使用影像、繪圖和視覺效果繪製

另外請注意,雖然 Stretch 屬性看起來像是指定 TileBrush 內容如何延伸填滿其輸出區域,但實際上是指定 TileBrush 內容如何延伸填滿其基底並排顯示。如需詳細資訊,請參閱 TileBrush

這個程式碼範例是 ImageBrush 類別完整範例的一部分。如需完整範例,請參閱 ImageBrush 範例

請參閱

概念

使用影像、繪圖和視覺效果繪製

參考

TileBrush