Export (0) Print
Expand All

How to: Transform a Brush

This example shows how to transform Brush objects by using their two transformation properties: RelativeTransform and Transform.

The following examples use a RotateTransform to rotate the content of an ImageBrush by 45 degrees.

The following illustration shows the ImageBrush without a RotateTransform, with the RotateTransform applied to the RelativeTransform property, and with the RotateTransform applied to the Transform property.

Brush RelativeTransform and Transform settings

The first example applies a RotateTransform to the RelativeTransform property of an ImageBrush. The CenterX and CenterY properties of a RotateTransform object are both set to 0.5, which is the relative coordinate of the center point of this content. As a result, the ImageBrush content rotates about its center.

			'
			' Create an ImageBrush with a relative transform and
			' use it to paint a rectangle.
			'
			Dim relativeTransformImageBrush As New ImageBrush()
			relativeTransformImageBrush.ImageSource = New BitmapImage(New Uri("sampleImages\pinkcherries.jpg", UriKind.Relative))

			' Create a 45 rotate transform about the brush's center
			' and apply it to the brush's RelativeTransform property.
			Dim aRotateTransform As New RotateTransform()
			aRotateTransform.CenterX = 0.5
			aRotateTransform.CenterY = 0.5
			aRotateTransform.Angle = 45
			relativeTransformImageBrush.RelativeTransform = aRotateTransform

			' Use the brush to paint a rectangle.
			Dim relativeTransformImageBrushRectangle As New Rectangle()
			relativeTransformImageBrushRectangle.Width = 175
			relativeTransformImageBrushRectangle.Height = 90
			relativeTransformImageBrushRectangle.Stroke = Brushes.Black
			relativeTransformImageBrushRectangle.Fill = relativeTransformImageBrush
<Rectangle Width="175" Height="90" Stroke="Black">
  <Rectangle.Fill>
    <ImageBrush ImageSource="sampleImages\pinkcherries.jpg">
      <ImageBrush.RelativeTransform>
        <RotateTransform CenterX="0.5" CenterY="0.5" Angle="45" />
      </ImageBrush.RelativeTransform>
    </ImageBrush>
  </Rectangle.Fill>
</Rectangle>

The second example also applies a RotateTransform to an ImageBrush; however, this example uses the Transform property instead of the RelativeTransform property.

To rotate the brush about its center, the example sets the CenterX and CenterY properties of the RotateTransform object to absolute coordinates. Because the brush paints a rectangle that is 175 by 90 pixels, the center point of the rectangle is (87.5, 45).

			'
			' Create an ImageBrush with a transform and
			' use it to paint a rectangle.
			'
			Dim transformImageBrush As New ImageBrush()
			transformImageBrush.ImageSource = New BitmapImage(New Uri("sampleImages\pinkcherries.jpg", UriKind.Relative))

			' Create a 45 rotate transform about the brush's center
			' and apply it to the brush's Transform property.
			Dim anotherRotateTransform As New RotateTransform()
			anotherRotateTransform.CenterX = 87.5
			anotherRotateTransform.CenterY = 45
			anotherRotateTransform.Angle = 45
			transformImageBrush.Transform = anotherRotateTransform

			' Use the brush to paint a rectangle.
			Dim transformImageBrushRectangle As New Rectangle()
			transformImageBrushRectangle.Width = 175
			transformImageBrushRectangle.Height = 90
			transformImageBrushRectangle.Stroke = Brushes.Black
			transformImageBrushRectangle.Fill = transformImageBrush
<Rectangle Width="175" Height="90" Stroke="Black">
  <Rectangle.Fill>
    <ImageBrush ImageSource="sampleImages\pinkcherries.jpg">
      <ImageBrush.Transform>
        <RotateTransform CenterX="87.5" CenterY="45" Angle="45" />
      </ImageBrush.Transform>
    </ImageBrush>
  </Rectangle.Fill>
</Rectangle>

For a description of how the RelativeTransform and Transform properties work, see the Brush Transformation Overview.

For the complete sample, see Brushes Sample. For more information about brushes, see Painting with Solid Colors and Gradients Overview.

Show:
© 2014 Microsoft