Cómo: Establecer el tamaño del mosaico de un TileBrush

En este ejemplo se muestra cómo establecer el tamaño del mosaico para TileBrush. De manera predeterminada, TileBrush genera un mosaico único que rellena completamente el área que se está pintando. Puede invalidar este comportamiento estableciendo las propiedades Viewport y ViewportUnits.

La propiedad Viewport especifica el tamaño del mosaico para TileBrush. De manera predeterminada, el valor de la propiedad Viewport es relativo al tamaño del área que se está pintando. Para que la propiedad Viewport especifique un tamaño del mosaico absoluto, establezca la propiedad ViewportUnits en Absolute.

Ejemplo

En el ejemplo siguiente se utiliza ImageBrush, un tipo de TileBrush, para pintar un rectángulo con mosaicos. En el ejemplo se establece el área de salida de cada mosaico en el 50% del alto y del ancho (el rectángulo). Como resultado, el rectángulo se pintado con cuatro proyecciones de la imagen.

En la ilustración siguiente se muestra el resultado de aplicar el ejemplo.

Ejemplo de disposición en mosaico con un pincel de imagen


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

En el ejemplo siguiente se crea ImageBrush, se establece Viewport en 0,0,25,25 y ViewportUnits en Absolute, y se lo utiliza para pintar otro rectángulo. Como resultado, el pincel genera mosaicos que tienen un ancho de 25 píxeles y un alto de 25 píxeles.

En la ilustración siguiente se muestra el resultado de aplicar el ejemplo.

TileBrush en mosaico con una ventanilla de 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>

Los ejemplos anteriores forman parte de un ejemplo mayor. Para obtener el ejemplo completo, vea ImageBrush Sample.

Aunque en este ejemplo se utiliza la clase ImageBrush, las propiedades Viewport y ViewportUnits se comportan exactamente igual para los demás objetos TileBrush, es decir, para DrawingBrush y VisualBrush. Para obtener más información sobre ImageBrush y los demás objetos TileBrush, consulte Pintar con imágenes, dibujos y elementos visuales.

Vea también

Tareas

Cómo: Crear patrones de mosaico diferentes con un objeto TileBrush

Referencia

TileBrush

Conceptos

Pintar con imágenes, dibujos y elementos visuales