HOW TO:設定 TileBrush 的並排顯示大小
更新:2007 年 11 月
本範例說明如何設定 TileBrush 的並排顯示大小。根據預設,TileBrush 會產生單一並排顯示來完全填滿您正在繪製的區域。您可以設定 Viewport 和 ViewportUnits 屬性覆寫這項行為。
Viewport 屬性會指定 TileBrush 的並排顯示大小。根據預設,Viewport 屬性的值是相對於正在繪製的區域大小。若要設定 Viewport 屬性指定並排顯示的絕對大小,請將 ViewportUnits 屬性設為 Absolute。
範例
下列範例會使用 ImageBrush (TileBrush 的一種),以並排方式顯示繪製矩形。這個範例會將每個並排顯示設為輸出區域 (即矩形) 的 50 % x 50 %。因此,矩形是以影像的四個投影所繪製。
下圖顯示的是範例產生的輸出。
//
// 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 個像素的並排顯示。
下圖顯示的是範例產生的輸出。
//
// 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 範例。
雖然本範例使用了 ImageBrush 類別,但 Viewport 和 ViewportUnits 屬性對其他 TileBrush 物件 (即 DrawingBrush 和 VisualBrush) 的作用完全相同。如需 ImageBrush 和其他 TileBrush 物件的詳細資訊,請參閱使用影像、繪圖和視覺效果繪製。
請參閱
工作
HOW TO:使用 TileBrush 建立不同的並排顯示模式