共用方式為


操作說明:使用影像繪製區域

這個範例示範如何使用 ImageBrush 類別來繪製具有影像的區域。 會顯示 ImageBrush 由其 ImageSource 屬性指定的單一影像。

範例

下列範例會使用 ImageBrush 繪製 Background 按鈕的 。


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace Microsoft.Samples.Graphics.UsingImageBrush
{

    public class PaintingWithImagesExample : Page
    {

        public PaintingWithImagesExample()
        {
            Background = Brushes.White;
            StackPanel mainPanel = new StackPanel();
            mainPanel.Margin = new Thickness(20.0);

            // Create a button.
            Button berriesButton = new Button();
            berriesButton.Foreground = Brushes.White;
            berriesButton.FontWeight = FontWeights.Bold;
            FontSizeConverter sizeConverter = new FontSizeConverter();
            berriesButton.FontSize = (Double)sizeConverter.ConvertFromString("16pt");
            berriesButton.FontFamily = new FontFamily("Verdana");
            berriesButton.Content = "Berries";
            berriesButton.Padding = new Thickness(20.0);
            berriesButton.HorizontalAlignment = HorizontalAlignment.Left;

            // Create an ImageBrush.
            ImageBrush berriesBrush = new ImageBrush();
            berriesBrush.ImageSource =
                new BitmapImage(
                    new Uri(@"sampleImages\berries.jpg", UriKind.Relative)
                );

            // Use the brush to paint the button's background.
            berriesButton.Background = berriesBrush;

            mainPanel.Children.Add(berriesButton);
            this.Content = mainPanel;
        }
    }
}

根據預設,會 ImageBrush 延展其影像,以完全填滿您要繪製的區域。 在上一個範例中,已自動縮放影像來填滿按鈕,且可能造成影像扭曲。 您可以將 的 TileBrush 屬性設定 StretchUniformUniformToFill 來控制此行為,這會導致筆刷保留影像的外觀比例。

如果您設定 的 ViewportImageBrushTileMode 屬性,您可以建立重複模式。 下列範例使用從影像建立的圖樣來繪製按鈕。


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace Microsoft.Samples.Graphics.UsingImageBrush
{

    public class TiledImageBrushExample : Page
    {

        public TiledImageBrushExample()
        {
            Background = Brushes.White;
            StackPanel mainPanel = new StackPanel();
            mainPanel.Margin = new Thickness(20.0);

            // Create a button.
            Button berriesButton = new Button();
            berriesButton.Foreground = Brushes.White;
            berriesButton.FontWeight = FontWeights.Bold;
            FontSizeConverter sizeConverter = new FontSizeConverter();
            berriesButton.FontSize = (Double)sizeConverter.ConvertFromString("16pt");
            berriesButton.FontFamily = new FontFamily("Verdana");
            berriesButton.Content = "Berries";
            berriesButton.Padding = new Thickness(20.0);
            berriesButton.HorizontalAlignment = HorizontalAlignment.Left;

            // Create an ImageBrush.
            ImageBrush berriesBrush = new ImageBrush();
            berriesBrush.ImageSource =
                new BitmapImage(
                    new Uri(@"sampleImages\berries.jpg", UriKind.Relative)
                );

            // Set the ImageBrush's Viewport and TileMode
            // so that it produces a pattern from
            // the image.
            berriesBrush.Viewport = new Rect(0,0,0.5,0.5);
            berriesBrush.TileMode = TileMode.FlipXY;

            // Use the brush to paint the button's background.
            berriesButton.Background = berriesBrush;

            mainPanel.Children.Add(berriesButton);
            this.Content = mainPanel;
        }
    }
}

Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media.Imaging
Imports System.Windows.Media

Namespace Microsoft.Samples.Graphics.UsingImageBrush

    Public Class TiledImageBrushExample
        Inherits Page

        Public Sub New()
            Background = Brushes.White
            Dim mainPanel As New StackPanel()
            mainPanel.Margin = New Thickness(20.0)

            ' Create a button.
            Dim berriesButton As New Button()
            With berriesButton
                .Foreground = Brushes.White
                .FontWeight = FontWeights.Bold
                Dim sizeConverter As New FontSizeConverter()
                .FontSize = CType(sizeConverter.ConvertFromString("16pt"), Double)
                .FontFamily = New FontFamily("Verdana")
                .Content = "Berries"
                .Padding = New Thickness(20.0)
                .HorizontalAlignment = HorizontalAlignment.Left
            End With

            ' Create an ImageBrush.
            Dim berriesBrush As New ImageBrush()
            berriesBrush.ImageSource = New BitmapImage(New Uri("sampleImages\berries.jpg", UriKind.Relative))

            ' Set the ImageBrush's Viewport and TileMode
            ' so that it produces a pattern from
            ' the image.
            berriesBrush.Viewport = New Rect(0, 0, 0.5, 0.5)
            berriesBrush.TileMode = TileMode.FlipXY

            ' Use the brush to paint the button's background.
            berriesButton.Background = berriesBrush

            mainPanel.Children.Add(berriesButton)
            Me.Content = mainPanel
        End Sub
    End Class
End Namespace

如需 類別 ImageBrush 的詳細資訊,請參閱 使用影像、繪圖和視覺效果 小畫家。

此程式碼範例是針對 類別提供的較大範例的 ImageBrush 一部分。 如需完整的範例,請參閱 ImageBrush 範例

另請參閱