共用方式為


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

本主題說明如何使用 ImageBrushDrawingBrushVisualBrush 物件,以影像 DrawingVisual 繪製區域。

這個主題包含下列章節。

  • 必要條件
  • 使用影像繪製區域
  • 範例:使用點陣圖影像繪製物件
  • 使用繪圖繪製區域
  • 範例:使用繪圖繪製物件
  • 使用視覺效果繪製區域
  • 範例:使用視覺效果繪製物件
  • 範例:建立反映
  • TileBrush 功能
  • 相關主題

必要條件

若要了解本主題,您應該熟悉 Windows Presentation Foundation (WPF) 所提供的不同筆刷類型及其基本功能。 如需簡介,請參閱 WPF 筆刷概觀

使用影像繪製區域

ImageBrush 可以使用 ImageSource 來繪製區域。 最常搭配 ImageBrush 使用的 ImageSource 類型是 BitmapImage,它可以描述點陣圖形。 使用 DrawingImage 可讓您以 Drawing 物件進行繪製,但使用 DrawingBrush 比較簡單。 如需 ImageSource 物件的詳細資訊,請參閱影像處理概觀

若要使用 ImageBrush 進行繪製,請建立 BitmapImage 並用它載入點陣圖內容。 接著,請使用 BitmapImage 來設定 ImageBrushImageSource 屬性。 最後,將 ImageBrush 套用至您要繪製的物件。 在Extensible Application Markup Language (XAML) 中,您也可以使用要載入之影像的路徑,設定 ImageBrushImageSource 屬性。

就像所有 Brush 物件,ImageBrush 可以用於繪製圖案、面板、控制項與文字等物件。 下圖顯示可以使用 ImageBrush 達成的部分效果。

ImageBrush 所繪製的物件

ImageBrush 輸出範例

根據預設,ImageBrush 會自動縮放其影像的大小以完全填滿繪製的區域,此時如果繪製的區域與影像所使用的外觀比例不相同,影像可能會變形。 您可以將 Stretch 屬性的預設值 Fill 變更為 NoneUniformUniformToFill,以改變這個行為。 由於 ImageBrush 是一種 TileBrush,因此您可以確切指定影像筆刷填滿輸出區域的方式,甚至建立圖樣。 如需進階 TileBrush 功能的詳細資訊,請參閱 TileBrush 概觀

範例:使用點陣圖影像繪製物件

下列範例是使用 ImageBrush 來繪製 CanvasBackground

<Page
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="Microsoft.Samples.BrushExamples.ImageBrushExample"
  WindowTitle="ImageBrush Example"
  Background="White">

  <StackPanel>

    <Canvas
      Height="200" Width="300">
      <Canvas.Background>
        <ImageBrush ImageSource="sampleImages\Waterlilies.jpg" />
      </Canvas.Background>
    </Canvas>


  </StackPanel>
</Page>

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Imaging

Namespace Microsoft.Samples.BrushExamples

    Public Class ImageBrushExample
        Inherits Page

        Public Sub New()

            Dim mainPanel As New StackPanel()
            canvasBackgroundExample(mainPanel)
            Me.Content = mainPanel

        End Sub


        Private Sub canvasBackgroundExample(ByVal mainPanel As Panel)

            Dim theImage As New BitmapImage(New Uri("sampleImages\Waterlilies.jpg", UriKind.Relative))

            Dim myImageBrush As New ImageBrush(theImage)

            Dim myCanvas As New Canvas()
            myCanvas.Width = 300
            myCanvas.Height = 200
            myCanvas.Background = myImageBrush

            mainPanel.Children.Add(myCanvas)


        End Sub

    End Class

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

namespace Microsoft.Samples.BrushExamples
{

    public class ImageBrushExample : Page
    {

        public ImageBrushExample()
        {

            StackPanel mainPanel = new StackPanel();
            canvasBackgroundExample(mainPanel);
            this.Content = mainPanel;

        }


        private void canvasBackgroundExample(Panel mainPanel)
        {

            BitmapImage theImage = new BitmapImage
                (new Uri("sampleImages\\Waterlilies.jpg", UriKind.Relative));

            ImageBrush myImageBrush = new ImageBrush(theImage);

            Canvas myCanvas = new Canvas();
            myCanvas.Width = 300;
            myCanvas.Height = 200;
            myCanvas.Background = myImageBrush;

            mainPanel.Children.Add(myCanvas);


        }

    }

}

使用繪圖繪製區域

DrawingBrush 可讓您使用圖案、文字、影像與視訊來繪製區域。 繪圖筆刷內的圖案本身就可以用純色、漸層、影像或其他 DrawingBrush 來繪製。 下圖示範 DrawingBrush 的一些用法。

DrawingBrush 所繪製的物件

DrawingBrush 輸出範例

DrawingBrush 可以使用 Drawing 物件繪製區域。 Drawing 物件會描述可見內容,例如圖案、點陣圖、視訊或文字行。 不同類型的繪圖可描繪不同類型的內容。 以下列出不同類型的繪圖物件。

如需 Drawing 物件的詳細資訊,請參閱繪圖物件概觀

就像 ImageBrush 一樣,DrawingBrush 會自動縮放其 Drawing 以填滿其輸出區域。 您可以變更 Stretch 屬性的預設設定 Fill,以覆寫這個行為。 如需詳細資訊,請參閱 Stretch 屬性 (Property)。

範例:使用繪圖繪製物件

下列範例顯示如何使用含有三個橢圓形的繪圖來繪製物件。 GeometryDrawing 是用以描述橢圓形。

<Button Content="A Button">
  <Button.Background>
    <DrawingBrush>
      <DrawingBrush.Drawing>
        <GeometryDrawing Brush="LightBlue">
          <GeometryDrawing.Geometry>
            <GeometryGroup>
              <EllipseGeometry RadiusX="12.5" RadiusY="25" Center="25,50" />
              <EllipseGeometry RadiusX="12.5" RadiusY="25" Center="50,50" />
              <EllipseGeometry RadiusX="12.5" RadiusY="25" Center="75,50" />
            </GeometryGroup>
          </GeometryDrawing.Geometry>
          <GeometryDrawing.Pen>
            <Pen Thickness="1" Brush="Gray" />
          </GeometryDrawing.Pen>
        </GeometryDrawing>
      </DrawingBrush.Drawing>
    </DrawingBrush>
  </Button.Background>
</Button>
            ' Create a DrawingBrush.
            Dim myDrawingBrush As New DrawingBrush()

            ' Create a drawing.
            Dim myGeometryDrawing As New GeometryDrawing()
            myGeometryDrawing.Brush = Brushes.LightBlue
            myGeometryDrawing.Pen = New Pen(Brushes.Gray, 1)
            Dim ellipses As New GeometryGroup()
            ellipses.Children.Add(New EllipseGeometry(New Point(25,50), 12.5, 25))
            ellipses.Children.Add(New EllipseGeometry(New Point(50,50), 12.5, 25))
            ellipses.Children.Add(New EllipseGeometry(New Point(75,50), 12.5, 25))

            myGeometryDrawing.Geometry = ellipses
            myDrawingBrush.Drawing = myGeometryDrawing

            Dim myButton As New Button()
            myButton.Content = "A Button"

            ' Use the DrawingBrush to paint the button's background.
            myButton.Background = myDrawingBrush
// Create a DrawingBrush.
DrawingBrush myDrawingBrush = new DrawingBrush();

// Create a drawing.
GeometryDrawing myGeometryDrawing = new GeometryDrawing();
myGeometryDrawing.Brush = Brushes.LightBlue;
myGeometryDrawing.Pen = new Pen(Brushes.Gray, 1);
GeometryGroup ellipses = new GeometryGroup();
ellipses.Children.Add(new EllipseGeometry(new Point(25,50), 12.5, 25));
ellipses.Children.Add(new EllipseGeometry(new Point(50,50), 12.5, 25));
ellipses.Children.Add(new EllipseGeometry(new Point(75,50), 12.5, 25));

myGeometryDrawing.Geometry = ellipses;
myDrawingBrush.Drawing = myGeometryDrawing;

Button myButton = new Button();
myButton.Content = "A Button";

// Use the DrawingBrush to paint the button's background.
myButton.Background = myDrawingBrush;

使用視覺效果繪製區域

VisualBrush 是所有筆刷中功能最多樣化而強大的一種,可以使用 Visual 繪製區域。 Visual 是一種低階的圖形類型,可做為多種實用圖形元件的祖系。 例如,WindowFrameworkElementControl 類別都是一種 Visual 物件。 透過 VisualBrush,您可以使用絕大多數的 Windows Presentation Foundation (WPF) 圖形物件繪製區域。

注意事項注意事項

雖然 VisualBrush 是一種 Freezable 物件,但它在其 Visual 屬性是設為 null 以外的值時,將無法凍結 (設為唯讀)。

有兩種方式可以指定 VisualBrushVisual 內容。

  • 建立新的 Visual,並用它來設定 VisualBrushVisual 屬性。 例如,請參閱稍後的範例:使用視覺效果繪製物件一節。

  • 使用現有的 Visual,它會建立目標 Visual 的重複影像。 然後您就可以使用 VisualBrush 建立有趣的效果,例如反映與放大。 如需範例,請參閱範例:建立反映一節。

當您為 VisualBrush 定義新的 Visual,且 VisualUIElement (如面板或控制項) 時,如果 AutoLayoutContent 屬性設為 true,配置系統即會在 UIElement 及其子項目上執行。 但是根 UIElement 會獨立於系統的其他部分之外:樣式與外部配置無發法穿透此界限。 因此,您應該明確指定根 UIElement 的大小,因為它唯一的父系為 VisualBrush,所以它無法根據繪製的區域自動調整本身的大小。 如需 Windows Presentation Foundation (WPF) 中之配置的詳細資訊,請參閱配置系統

就像 ImageBrushDrawingBrush 一樣,VisualBrush 也會自動縮放其內容以填滿其輸出區域。 您可以變更 Stretch 屬性的預設設定 Fill,以覆寫這個行為。 如需詳細資訊,請參閱 Stretch 屬性 (Property)。

範例:使用視覺效果繪製物件

下列範例會使用多個控制項與一個面板來繪製矩形。

<Rectangle Width="150" Height="150" Stroke="Black" Margin="5,0,5,0">
  <Rectangle.Fill>
    <VisualBrush>
      <VisualBrush.Visual>
        <StackPanel Background="White">
          <Rectangle Width="25" Height="25" Fill="Red" Margin="2" />
          <TextBlock FontSize="10pt" Margin="2">Hello, World!</TextBlock>
          <Button Margin="2">A Button</Button>
        </StackPanel>
      </VisualBrush.Visual>
    </VisualBrush>
  </Rectangle.Fill>
</Rectangle>
            Dim myVisualBrush As New VisualBrush()

            ' Create the visual brush's contents.
            Dim myStackPanel As New StackPanel()
            myStackPanel.Background = Brushes.White

            Dim redRectangle As New Rectangle()
            With redRectangle
                .Width = 25
                .Height = 25
                .Fill = Brushes.Red
                .Margin = New Thickness(2)
            End With
            myStackPanel.Children.Add(redRectangle)

            Dim someText As New TextBlock()
            Dim myFontSizeConverter As New FontSizeConverter()
            someText.FontSize = CDbl(myFontSizeConverter.ConvertFrom("10pt"))
            someText.Text = "Hello, World!"
            someText.Margin = New Thickness(2)
            myStackPanel.Children.Add(someText)

            Dim aButton As New Button()
            aButton.Content = "A Button"
            aButton.Margin = New Thickness(2)
            myStackPanel.Children.Add(aButton)

            ' Use myStackPanel as myVisualBrush's content.
            myVisualBrush.Visual = myStackPanel

            ' Create a rectangle to paint.
            Dim myRectangle As New Rectangle()
            With myRectangle
                .Width = 150
                .Height = 150
                .Stroke = Brushes.Black
                .Margin = New Thickness(5, 0, 5, 0)
            End With

            ' Use myVisualBrush to paint myRectangle.
            myRectangle.Fill = myVisualBrush

            VisualBrush myVisualBrush = new VisualBrush();

            // Create the visual brush's contents.
            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Background = Brushes.White;

            Rectangle redRectangle = new Rectangle();
            redRectangle.Width = 25;
            redRectangle.Height =25; 
            redRectangle.Fill = Brushes.Red;
            redRectangle.Margin = new Thickness(2);
            myStackPanel.Children.Add(redRectangle);

            TextBlock someText = new TextBlock();
            FontSizeConverter myFontSizeConverter = new FontSizeConverter();
            someText.FontSize = (double)myFontSizeConverter.ConvertFrom("10pt");
            someText.Text = "Hello, World!";
            someText.Margin = new Thickness(2);
            myStackPanel.Children.Add(someText);

            Button aButton = new Button();
            aButton.Content = "A Button";
            aButton.Margin = new Thickness(2);
            myStackPanel.Children.Add(aButton);

            // Use myStackPanel as myVisualBrush's content.
            myVisualBrush.Visual = myStackPanel;

            // Create a rectangle to paint.
            Rectangle myRectangle = new Rectangle();
            myRectangle.Width = 150;
            myRectangle.Height = 150;
            myRectangle.Stroke = Brushes.Black;
            myRectangle.Margin = new Thickness(5,0,5,0);

            // Use myVisualBrush to paint myRectangle.
            myRectangle.Fill = myVisualBrush;

範例:建立反映

前述範例顯示了如何建立要做為背景 (Background) 的新 Visual。 您也可以使用 VisualBrush 來顯示現有的視覺效果;這項功能可讓您產生有趣的視覺效果,例如反映與放大。 下列範例會使用 VisualBrush 來建立包含幾個項目之 Border 的反映。 下圖顯示的是這個範例產生的輸出。

反映的 Visual 物件

反映後的 Visual 物件


Imports System
Imports System.Windows
Imports System.Windows.Data
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Effects
Imports System.Windows.Media.Imaging
Imports System.IO
Imports System.Collections.ObjectModel
Imports System.Windows.Shapes
Namespace SDKSample
    Partial Public Class ReflectionExample
        Inherits Page
        Public Sub New()
            ' Create a name scope for the page.
            NameScope.SetNameScope(Me, New NameScope())

            Me.Background = Brushes.Black
            Dim myStackPanel As New StackPanel()
            myStackPanel.Margin = New Thickness(50)

            Dim myReflectedBorder As New Border()
            Me.RegisterName("ReflectedVisual", myReflectedBorder)

            ' Create a gradient background for the border.
            Dim firstStop As New GradientStop()
            firstStop.Offset = 0.0
            Dim firstStopColor As New Color()
            firstStopColor.R = 204
            firstStopColor.G = 204
            firstStopColor.B = 255
            firstStopColor.A = 255
            firstStop.Color = firstStopColor
            Dim secondStop As New GradientStop()
            secondStop.Offset = 1.0
            secondStop.Color = Colors.White

            Dim myGradientStopCollection As New GradientStopCollection()
            myGradientStopCollection.Add(firstStop)
            myGradientStopCollection.Add(secondStop)

            Dim myLinearGradientBrush As New LinearGradientBrush()
            myLinearGradientBrush.StartPoint = New Point(0, 0.5)
            myLinearGradientBrush.EndPoint = New Point(1, 0.5)
            myLinearGradientBrush.GradientStops = myGradientStopCollection

            myReflectedBorder.Background = myLinearGradientBrush

            ' Add contents to the border.
            Dim borderStackPanel As New StackPanel()
            borderStackPanel.Orientation = Orientation.Horizontal
            borderStackPanel.Margin = New Thickness(10)

            Dim myTextBlock As New TextBlock()
            myTextBlock.TextWrapping = TextWrapping.Wrap
            myTextBlock.Width = 200
            myTextBlock.Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit." & " Suspendisse vel ante. Donec luctus tortor sit amet est." & " Nullam pulvinar odio et wisi." & " Pellentesque quis magna. Sed pellentesque." & " Nulla euismod." & "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas."

            borderStackPanel.Children.Add(myTextBlock)

            Dim ellipseStackPanel As New StackPanel()

            Dim ellipse1 As New Ellipse()
            ellipse1.Margin = New Thickness(10)
            ellipse1.Height = 50
            ellipse1.Width = 50
            ellipse1.Fill = Brushes.Black
            ellipseStackPanel.Children.Add(ellipse1)
            Dim ellipse2 As New Ellipse()
            ellipse2.Margin = New Thickness(10)
            ellipse2.Height = 50
            ellipse2.Width = 50
            ellipse2.Fill = Brushes.Black
            ellipseStackPanel.Children.Add(ellipse2)
            Dim ellipse3 As New Ellipse()
            ellipse3.Margin = New Thickness(10)
            ellipse3.Height = 50
            ellipse3.Width = 50
            ellipse3.Fill = Brushes.Black
            ellipseStackPanel.Children.Add(ellipse3)
            borderStackPanel.Children.Add(ellipseStackPanel)

            myReflectedBorder.Child = borderStackPanel

            ' Create divider rectangle
            Dim dividerRectangle As New Rectangle()
            dividerRectangle.Height = 1
            dividerRectangle.Fill = Brushes.Gray
            dividerRectangle.HorizontalAlignment = HorizontalAlignment.Stretch

            ' Create the object to contain the reflection.
            Dim reflectionRectangle As New Rectangle()

            ' Bind the height of the rectangle to the border height.
            Dim heightBinding As New Binding()
            heightBinding.ElementName = "ReflectedVisual"
            heightBinding.Path = New PropertyPath(Rectangle.HeightProperty)
            BindingOperations.SetBinding(reflectionRectangle, Rectangle.HeightProperty, heightBinding)

            ' Bind the width of the rectangle to the border width.
            Dim widthBinding As New Binding()
            widthBinding.ElementName = "ReflectedVisual"
            widthBinding.Path = New PropertyPath(Rectangle.WidthProperty)
            BindingOperations.SetBinding(reflectionRectangle, Rectangle.WidthProperty, widthBinding)

            ' Creates the reflection.
            Dim myVisualBrush As New VisualBrush()
            myVisualBrush.Opacity = 0.75
            myVisualBrush.Stretch = Stretch.None
            Dim reflectionBinding As New Binding()
            reflectionBinding.ElementName = "ReflectedVisual"
            BindingOperations.SetBinding(myVisualBrush, VisualBrush.VisualProperty, reflectionBinding)

            Dim myScaleTransform As New ScaleTransform()
            myScaleTransform.ScaleX = 1
            myScaleTransform.ScaleY = -1
            Dim myTranslateTransform As New TranslateTransform()
            myTranslateTransform.Y = 1

            Dim myTransformGroup As New TransformGroup()
            myTransformGroup.Children.Add(myScaleTransform)
            myTransformGroup.Children.Add(myTranslateTransform)

            myVisualBrush.RelativeTransform = myTransformGroup

            reflectionRectangle.Fill = myVisualBrush

            ' Create a gradient background for the border.
            Dim firstStop2 As New GradientStop()
            firstStop2.Offset = 0.0
            Dim c1 As New Color()
            c1.R = 0
            c1.G = 0
            c1.B = 0
            c1.A = 255
            firstStop2.Color = c1
            Dim secondStop2 As New GradientStop()
            secondStop2.Offset = 0.5
            Dim c2 As New Color()
            c2.R = 0
            c2.G = 0
            c2.B = 0
            c2.A = 51
            firstStop2.Color = c2
            Dim thirdStop As New GradientStop()
            thirdStop.Offset = 0.75
            Dim c3 As New Color()
            c3.R = 0
            c3.G = 0
            c3.B = 0
            c3.A = 0
            thirdStop.Color = c3

            Dim myGradientStopCollection2 As New GradientStopCollection()
            myGradientStopCollection2.Add(firstStop2)
            myGradientStopCollection2.Add(secondStop2)
            myGradientStopCollection2.Add(thirdStop)

            Dim myLinearGradientBrush2 As New LinearGradientBrush()
            myLinearGradientBrush2.StartPoint = New Point(0.5, 0)
            myLinearGradientBrush2.EndPoint = New Point(0.5, 1)
            myLinearGradientBrush2.GradientStops = myGradientStopCollection2

            reflectionRectangle.OpacityMask = myLinearGradientBrush2

            Dim myBlurBitmapEffect As New BlurBitmapEffect()
            myBlurBitmapEffect.Radius = 1.5

            reflectionRectangle.BitmapEffect = myBlurBitmapEffect

            myStackPanel.Children.Add(myReflectedBorder)
            myStackPanel.Children.Add(dividerRectangle)
            myStackPanel.Children.Add(reflectionRectangle)
            Me.Content = myStackPanel

        End Sub
    End Class
End Namespace
using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.IO;
using System.Collections.ObjectModel;
using System.Windows.Shapes;
namespace SDKSample
{
    public partial class ReflectionExample : Page
    {
        public ReflectionExample()
        {
            // Create a name scope for the page.
            NameScope.SetNameScope(this, new NameScope());

            this.Background = Brushes.Black;
            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Margin = new Thickness(50);

            Border myReflectedBorder = new Border();
            this.RegisterName("ReflectedVisual", myReflectedBorder);

            // Create a gradient background for the border.
            GradientStop firstStop = new GradientStop();
            firstStop.Offset = 0.0;
            Color firstStopColor = new Color();
            firstStopColor.R = 204;
            firstStopColor.G = 204;
            firstStopColor.B = 255;
            firstStopColor.A = 255;
            firstStop.Color = firstStopColor;
            GradientStop secondStop = new GradientStop();
            secondStop.Offset = 1.0;
            secondStop.Color = Colors.White;

            GradientStopCollection myGradientStopCollection = new GradientStopCollection();
            myGradientStopCollection.Add(firstStop);
            myGradientStopCollection.Add(secondStop);

            LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
            myLinearGradientBrush.StartPoint = new Point(0, 0.5);
            myLinearGradientBrush.EndPoint = new Point(1, 0.5);
            myLinearGradientBrush.GradientStops = myGradientStopCollection;

            myReflectedBorder.Background = myLinearGradientBrush;

            // Add contents to the border.
            StackPanel borderStackPanel = new StackPanel();
            borderStackPanel.Orientation = Orientation.Horizontal;
            borderStackPanel.Margin = new Thickness(10);

            TextBlock myTextBlock = new TextBlock();
            myTextBlock.TextWrapping = TextWrapping.Wrap;
            myTextBlock.Width = 200;
            myTextBlock.Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit." +
                               " Suspendisse vel ante. Donec luctus tortor sit amet est." +
                               " Nullam pulvinar odio et wisi." +
                               " Pellentesque quis magna. Sed pellentesque." +
                               " Nulla euismod." +
                               "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.";

            borderStackPanel.Children.Add(myTextBlock);

            StackPanel ellipseStackPanel = new StackPanel();

            Ellipse ellipse1 = new Ellipse();
            ellipse1.Margin = new Thickness(10);
            ellipse1.Height = 50;
            ellipse1.Width = 50;
            ellipse1.Fill = Brushes.Black;
            ellipseStackPanel.Children.Add(ellipse1);
            Ellipse ellipse2 = new Ellipse();
            ellipse2.Margin = new Thickness(10);
            ellipse2.Height = 50;
            ellipse2.Width = 50;
            ellipse2.Fill = Brushes.Black;
            ellipseStackPanel.Children.Add(ellipse2);
            Ellipse ellipse3 = new Ellipse();
            ellipse3.Margin = new Thickness(10);
            ellipse3.Height = 50;
            ellipse3.Width = 50;
            ellipse3.Fill = Brushes.Black;
            ellipseStackPanel.Children.Add(ellipse3);
            borderStackPanel.Children.Add(ellipseStackPanel);

            myReflectedBorder.Child = borderStackPanel;

            // Create divider rectangle
            Rectangle dividerRectangle = new Rectangle();
            dividerRectangle.Height = 1;
            dividerRectangle.Fill = Brushes.Gray;
            dividerRectangle.HorizontalAlignment = HorizontalAlignment.Stretch;

            // Create the object to contain the reflection.
            Rectangle reflectionRectangle = new Rectangle();

            // Bind the height of the rectangle to the border height.
            Binding heightBinding = new Binding();
            heightBinding.ElementName = "ReflectedVisual";
            heightBinding.Path = new PropertyPath(Rectangle.HeightProperty);
            BindingOperations.SetBinding(reflectionRectangle, Rectangle.HeightProperty, heightBinding);

            // Bind the width of the rectangle to the border width.
            Binding widthBinding = new Binding();
            widthBinding.ElementName = "ReflectedVisual";
            widthBinding.Path = new PropertyPath(Rectangle.WidthProperty);
            BindingOperations.SetBinding(reflectionRectangle, Rectangle.WidthProperty, widthBinding);

            // Creates the reflection.
            VisualBrush myVisualBrush = new VisualBrush();
            myVisualBrush.Opacity = 0.75;
            myVisualBrush.Stretch = Stretch.None;
            Binding reflectionBinding = new Binding();
            reflectionBinding.ElementName = "ReflectedVisual";
            BindingOperations.SetBinding(myVisualBrush, VisualBrush.VisualProperty, reflectionBinding);

            ScaleTransform myScaleTransform = new ScaleTransform();
            myScaleTransform.ScaleX = 1;
            myScaleTransform.ScaleY = -1;
            TranslateTransform myTranslateTransform = new TranslateTransform();
            myTranslateTransform.Y = 1;

            TransformGroup myTransformGroup = new TransformGroup();
            myTransformGroup.Children.Add(myScaleTransform);
            myTransformGroup.Children.Add(myTranslateTransform);

            myVisualBrush.RelativeTransform = myTransformGroup;

            reflectionRectangle.Fill = myVisualBrush;

            // Create a gradient background for the border.
            GradientStop firstStop2 = new GradientStop();
            firstStop2.Offset = 0.0;
            Color c1 = new Color();
            c1.R = 0;
            c1.G = 0;
            c1.B = 0;
            c1.A = 255;
            firstStop2.Color = c1;
            GradientStop secondStop2 = new GradientStop();
            secondStop2.Offset = 0.5;
            Color c2 = new Color();
            c2.R = 0;
            c2.G = 0;
            c2.B = 0;
            c2.A = 51;
            firstStop2.Color = c2;
            GradientStop thirdStop = new GradientStop();
            thirdStop.Offset = 0.75;
            Color c3 = new Color();
            c3.R = 0;
            c3.G = 0;
            c3.B = 0;
            c3.A = 0;
            thirdStop.Color = c3;

            GradientStopCollection myGradientStopCollection2 = new GradientStopCollection();
            myGradientStopCollection2.Add(firstStop2);
            myGradientStopCollection2.Add(secondStop2);
            myGradientStopCollection2.Add(thirdStop);

            LinearGradientBrush myLinearGradientBrush2 = new LinearGradientBrush();
            myLinearGradientBrush2.StartPoint = new Point(0.5, 0);
            myLinearGradientBrush2.EndPoint = new Point(0.5, 1);
            myLinearGradientBrush2.GradientStops = myGradientStopCollection2;

            reflectionRectangle.OpacityMask = myLinearGradientBrush2;

            BlurBitmapEffect myBlurBitmapEffect = new BlurBitmapEffect();
            myBlurBitmapEffect.Radius = 1.5;

            reflectionRectangle.BitmapEffect = myBlurBitmapEffect;

            myStackPanel.Children.Add(myReflectedBorder);
            myStackPanel.Children.Add(dividerRectangle);
            myStackPanel.Children.Add(reflectionRectangle);
            this.Content = myStackPanel;

        }
        /*
    <Rectangle 
      Height="{Binding Path=ActualHeight, ElementName=ReflectedVisual}" 
      Width="{Binding Path=ActualWidth, ElementName=ReflectedVisual}">

      <Rectangle.OpacityMask>
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
          <GradientStop Color="#FF000000" Offset="0.0" />
          <GradientStop Color="#33000000" Offset="0.5" />
          <GradientStop Color="#00000000" Offset="0.75" />
        </LinearGradientBrush>
      </Rectangle.OpacityMask>

      <Rectangle.BitmapEffect>
        <BlurBitmapEffect Radius="1.5" />
      </Rectangle.BitmapEffect>

    </Rectangle>
  </StackPanel>
</Page>

*/

    }
}
<Page  
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
  Background="Black">


  <StackPanel Margin="50">

    <!-- The object to reflect. -->
    <Border Name="ReflectedVisual" Width="400">
      <Border.Background>
        <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
          <GradientStop Offset="0.0" Color="#CCCCFF" />
          <GradientStop Offset="1.0" Color="White" />
        </LinearGradientBrush>
      </Border.Background>
      <StackPanel Orientation="Horizontal" Margin="10">        
        <TextBlock TextWrapping="Wrap" Width="200" Margin="10">
          Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
          Suspendisse vel ante. Donec luctus tortor sit amet est.
          Nullam pulvinar odio et wisi.
          Pellentesque quis magna. Sed pellentesque.
          Nulla euismod.
          Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
        </TextBlock>
        <StackPanel>
          <Ellipse Margin="10" Height="50" Width="50" Fill="Black" />
          <Ellipse Margin="10" Height="50" Width="50" Fill="Black" />
          <Ellipse Margin="10" Height="50" Width="50" Fill="Black" />
        </StackPanel>
      </StackPanel>
    </Border>

    <Rectangle Height="1" Fill="Gray" HorizontalAlignment="Stretch" />

    <!-- The object to contain the reflection.-->
    <Rectangle 
      Height="{Binding Path=ActualHeight, ElementName=ReflectedVisual}" 
      Width="{Binding Path=ActualWidth, ElementName=ReflectedVisual}">
      <Rectangle.Fill>

        <!-- Creates the reflection. -->
        <VisualBrush 
          Opacity="0.75" Stretch="None"
          Visual="{Binding ElementName=ReflectedVisual}">
          <VisualBrush.RelativeTransform>

            <!-- Flip the reflection. -->
            <TransformGroup>
              <ScaleTransform ScaleX="1" ScaleY="-1" />
              <TranslateTransform  Y="1" />
            </TransformGroup>
          </VisualBrush.RelativeTransform>
        </VisualBrush>
      </Rectangle.Fill>

      <Rectangle.OpacityMask>
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
          <GradientStop Color="#FF000000" Offset="0.0" />
          <GradientStop Color="#33000000" Offset="0.5" />
          <GradientStop Color="#00000000" Offset="0.75" />
        </LinearGradientBrush>
      </Rectangle.OpacityMask>

      <Rectangle.BitmapEffect>
        <BlurBitmapEffect Radius="1.5" />
      </Rectangle.BitmapEffect>

    </Rectangle>
  </StackPanel>
</Page>

如需示範如何放大部分螢幕以及如何建立反映的其他範例,請參閱 VisualBrush 範例 (英文)。

TileBrush 功能

ImageBrushDrawingBrushVisualBrushTileBrush 物件的型別。 TileBrush 物件可讓您更充分掌控使用影像、繪圖或視覺資料繪製區域的方式。 例如,您可以用構成圖樣的一系列影像並排顯示來繪製區域,而不是只以單一自動縮放的影像來繪製區域。

TileBrush 有三個主要的元件:內容、並排顯示與輸出區域。

具有單一並排顯示之 TileBrush 的元件

TileBrush 元件

具有多個並排顯示之 TileBrush 的元件

並排顯示之 TileBrush 的元件

如需 TileBrush 物件之並排顯示功能的詳細資訊,請參閱 TileBrush 概觀

請參閱

參考

ImageBrush

DrawingBrush

VisualBrush

TileBrush

概念

TileBrush 概觀

WPF 筆刷概觀

影像處理概觀

繪圖物件概觀

不透明遮罩概觀

WPF 圖形轉譯概觀

其他資源

ImageBrush 範例

VisualBrush Sample