如何:为 TileBrush 设置平铺大小

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

Viewport 属性指定 TileBrush 的平铺大小。 默认情况下,Viewport 属性与要绘制区域的大小有关。 若要使 Viewport 属性指定绝对平铺大小,请将 ViewportUnits 属性设置为 Absolute

示例

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

下图显示该示例生成的输出:

A rectangle with four cherries demonstrating tiling with an image brush.


//
// 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;

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

下图显示该示例生成的输出:

A rectangle with forty-eight cherries demonstrating a tiled TileBrush with a Viewport.


//
// 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;

以上几个示例摘自一个更大的示例。 有关完整示例,请参阅 ImageBrush 示例

虽然此示例使用 ImageBrush 类,但对于其他 TileBrush 对象(即 DrawingBrushVisualBrush),ViewportViewportUnits 属性的行为相同。 有关 ImageBrush 和其他 TileBrush 对象的详细信息,请参阅使用图像、绘图和视觉对象进行绘制

另请参阅