如何:为 TileBrush 设置平铺大小

下面的示例演示如何为 TileBrush 设置图块大小。 默认情况下,TileBrush 生成一个可完全填充所绘制区域的图块。 可以通过设置 ViewportViewportUnits 属性来重写此行为。

Viewport 属性可以为 TileBrush 指定图块大小。 默认情况下,Viewport 属性值相对于所绘制区域的大小。 为了让 Viewport 属性指定绝对图块大小,请将 ViewportUnits 属性设置为 Absolute

示例

下面的示例使用 ImageBrushTileBrush 的一个类型)来绘制带有图块的矩形。 此示例将每个图块图像设置为在长宽方向上分别占输出区域(矩形)的 50%。 因此,该矩形将用图块图像的四个投影来绘制。

下面的插图显示由该示例生成的输出。

使用图像画笔平铺的示例


            '
            ' Create an ImageBrush and set the size of each
            ' tile to 50% by 50% of the area being painted. 
            ' 
            Dim relativeTileSizeImageBrush As New ImageBrush()
            relativeTileSizeImageBrush.ImageSource = New BitmapImage(New Uri("sampleImages\cherries_larger.jpg", UriKind.Relative))
            relativeTileSizeImageBrush.TileMode = TileMode.Tile

            ' Specify the size of the base tile. 
            ' By default, the size of the Viewport is
            ' relative to the area being painted,
            ' so a value of 0.5 indicates 50% of the output
            ' area.
            relativeTileSizeImageBrush.Viewport = New Rect(0, 0, 0.5, 0.5)

            ' Create a rectangle and paint it with the ImageBrush.
            Dim relativeTileSizeExampleRectangle As New Rectangle()
            With relativeTileSizeExampleRectangle
                .Width = 200
                .Height = 150
                .Stroke = Brushes.LimeGreen
                .StrokeThickness = 1
                .Fill = relativeTileSizeImageBrush
            End With

//
// Create an ImageBrush and set the size of each
// tile to 50% by 50% of the area being painted. 
// 
ImageBrush relativeTileSizeImageBrush = new ImageBrush();
relativeTileSizeImageBrush.ImageSource =
    new BitmapImage(new Uri(@"sampleImages\cherries_larger.jpg", UriKind.Relative));
relativeTileSizeImageBrush.TileMode = TileMode.Tile;

// Specify the size of the base tile. 
// By default, the size of the Viewport is
// relative to the area being painted,
// so a value of 0.5 indicates 50% of the output
// area.
relativeTileSizeImageBrush.Viewport = new Rect(0, 0, 0.5, 0.5);

// Create a rectangle and paint it with the ImageBrush.
Rectangle relativeTileSizeExampleRectangle = new Rectangle();
relativeTileSizeExampleRectangle.Width = 200;
relativeTileSizeExampleRectangle.Height = 150;
relativeTileSizeExampleRectangle.Stroke = Brushes.LimeGreen;
relativeTileSizeExampleRectangle.StrokeThickness = 1;
relativeTileSizeExampleRectangle.Fill = relativeTileSizeImageBrush;
<!-- The ImageBrush's tiles are set to 50% by 50% of the output area. -->
<Rectangle
  Width="200" Height="150"
  Stroke="LimeGreen" StrokeThickness="1">
  <Rectangle.Fill>
    <ImageBrush
      Viewport="0,0,0.5,0.5"
      TileMode="Tile"
      ImageSource="sampleImages\cherries_larger.jpg" />
  </Rectangle.Fill>
</Rectangle>

下一个示例创建一个 ImageBrush,将它的 Viewport 设置为 0,0,25,25,将它的 ViewportUnits 设置为 Absolute,并使用它来绘制另一个矩形。 因此,画笔会生成一个宽度为 25 像素、高度为 25 像素的图块。

下面的插图显示由该示例生成的输出。

一个平铺的 TileBrush,其视区为 0,0,0.25,0.25


'
' Create an ImageBrush and set the size of each
' tile to 25 by 25 pixels. 
' 
Dim absoluteTileSizeImageBrush As New ImageBrush()
absoluteTileSizeImageBrush.ImageSource = New BitmapImage(New Uri("sampleImages\cherries_larger.jpg", UriKind.Relative))
absoluteTileSizeImageBrush.TileMode = TileMode.Tile

' Specify that the Viewport is to be interpreted as
' an absolute value. 
absoluteTileSizeImageBrush.ViewportUnits = BrushMappingMode.Absolute

' Set the size of the base tile. Had we left ViewportUnits set
' to RelativeToBoundingBox (the default value), 
' each tile would be 25 times the size of the area being
' painted. Because ViewportUnits is set to Absolute,
' the following line creates tiles that are 25 by 25 pixels.
absoluteTileSizeImageBrush.Viewport = New Rect(0, 0, 25, 25)

' Create a rectangle and paint it with the ImageBrush.
Dim absoluteTileSizeExampleRectangle As New Rectangle()
With absoluteTileSizeExampleRectangle
    .Width = 200
    .Height = 150
    .Stroke = Brushes.LimeGreen
    .StrokeThickness = 1
    .Fill = absoluteTileSizeImageBrush
End With

//
// Create an ImageBrush and set the size of each
// tile to 25 by 25 pixels. 
// 
ImageBrush absoluteTileSizeImageBrush = new ImageBrush();
absoluteTileSizeImageBrush.ImageSource =
     new BitmapImage(new Uri(@"sampleImages\cherries_larger.jpg", UriKind.Relative));
absoluteTileSizeImageBrush.TileMode = TileMode.Tile;

// Specify that the Viewport is to be interpreted as
// an absolute value. 
absoluteTileSizeImageBrush.ViewportUnits = BrushMappingMode.Absolute;

// Set the size of the base tile. Had we left ViewportUnits set
// to RelativeToBoundingBox (the default value), 
// each tile would be 25 times the size of the area being
// painted. Because ViewportUnits is set to Absolute,
// the following line creates tiles that are 25 by 25 pixels.
absoluteTileSizeImageBrush.Viewport = new Rect(0, 0, 25, 25);

// Create a rectangle and paint it with the ImageBrush.
Rectangle absoluteTileSizeExampleRectangle = new Rectangle();
absoluteTileSizeExampleRectangle.Width = 200;
absoluteTileSizeExampleRectangle.Height = 150;
absoluteTileSizeExampleRectangle.Stroke = Brushes.LimeGreen;
absoluteTileSizeExampleRectangle.StrokeThickness = 1;
absoluteTileSizeExampleRectangle.Fill = absoluteTileSizeImageBrush;
<!-- The ImageBrush's tiles are set to 25 by 25 pixels. -->
<Rectangle
  Width="200" Height="150"
  Stroke="LimeGreen" StrokeThickness="1">
  <Rectangle.Fill>
    <ImageBrush
      Viewport="0,0,25,25"
      ViewportUnits="Absolute"
      TileMode="Tile"
      ImageSource="sampleImages\cherries_larger.jpg" />
  </Rectangle.Fill>
</Rectangle>

上面的几个示例摘自一个更大的示例。 有关完整示例,请参见 ImageBrush Sample(ImageBrush 示例)。

尽管此示例使用 ImageBrush 类,但是 ViewportViewportUnits 属性对于其他 TileBrush 对象(即 DrawingBrushVisualBrush)的行为方式是相同的。 有关 ImageBrush 和其他 TileBrush 对象的更多信息,请参见使用图像、绘图和 Visual 进行绘制

请参见

任务

如何:使用 TileBrush 创建不同的平铺模式

参考

TileBrush

概念

使用图像、绘图和 Visual 进行绘制