Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TileBrush.ViewportUnits Property

Gets or sets a BrushMappingMode enumeration that specifies whether the value of the Viewport, which indicates the size and position of the TileBrush base tile, is relative to the size of the output area.

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

'Declaration
Public Property ViewportUnits As BrushMappingMode
<object ViewportUnits="BrushMappingMode" .../>

Property Value

Type: System.Windows.Media.BrushMappingMode
Indicates whether the value of the Viewport, which describes the size and position of the TileBrush tiles, is relative to the size of the whole output area. The default value is RelativeToBoundingBox.

Identifier field

ViewportUnitsProperty

Metadata properties set to true

None

The Viewport property determines the size and position of the tiles in a TileBrush. By default, a TileBrush has a single tile that fills the whole output area. The Stretch property controls how the TileBrush content fits into that tile. You can override this default behavior and specify the tile size by using the Viewport property.

Use the ViewportUnits property to specify whether the Viewport uses absolute or relative coordinates. If the coordinates are relative, they are relative to the size of the output area. The point (0,0) represents the upper-left corner of the output area, and (1,1) represents the lower-right corner of the output area. To specify that the Viewport property uses absolute coordinates, set the ViewportUnits property to Absolute.

The following illustration shows the difference in output when you set the ViewportUnits property of a TileBrush to either relative or absolute.

Relative and absolute ViewportUnits

Absolute and Relative Viewport Units

This example shows how to set the tile size for a TileBrush. By default, a TileBrush produces a single tile that completely fills the area that you are painting. You can override this behavior by setting the Viewport and ViewportUnits properties.

The Viewport property specifies the tile size for a TileBrush. By default, the value of the Viewport property is relative to the size of the area being painted. To make the Viewport property specify an absolute tile size, set the ViewportUnits property to Absolute.

The following example uses an ImageBrush, a type of TileBrush, to paint a rectangle with tiles. The example sets each tile to 50 percent by 50 percent of the output area (the rectangle). As a result, the rectangle is painted with four projections of the image.

The following illustration shows the output that the example produces.

Example of tiling with an image brush


			'
			' 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
<!-- 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>

The next example creates an ImageBrush, sets its Viewport to 0,0,25,25 and its ViewportUnits to Absolute, and uses it to paint another rectangle. As a result, the brush produces tiles that have a width of 25 pixels and a height of 25 pixels.

The following illustration shows the output that the example produces.

A tiled TileBrush with a Viewport of 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
<!-- 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>

The preceding examples are part of a larger sample. For the complete sample, see ImageBrush Sample.

Although this example uses the ImageBrush class, the Viewport and ViewportUnits properties behave identically for the other TileBrush objects, that is, for DrawingBrush and VisualBrush. For more information about ImageBrush and the other TileBrush objects, see Painting with Images, Drawings, and Visuals.

More Code

How to: Create Different Tile Patterns with a TileBrushThis example shows how to use the TileMode property of a TileBrush to create a pattern.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft