WPF RotateTransform in VB.NET

This article demonstrates how to rotate elements and controls in WPF using XAML and VB.NET.
  • 2992

RotateTransform rotates an element clockwise by a specified angle about the point. The RotateTransform object in WPF represents RotateTransform. The Angle property represents the angle in degrees to rotate clockwise. The CenterX and CenterY properties represent the X and Y coordinates of the center point. By default, a ScaleTransform is centered at the point (0,0), which corresponds to the upper-left corner of the rectangle.

The code listed in Listing creates two rectangles with same position and sizes accept the second rectangle is rotated at 45 degrees.

    <!-- Original Rectangle -->
    <Rectangle Width="200" Height="50" Fill="Yellow"/>          
<!-- Rectangle with 45 degrees rotation -->
    <Rectangle Width="200" Height="50" Fill="Blue" Opacity="0.5">
            <RotateTransform CenterX="0" CenterY="0" Angle="45" />

The output of Listing looks like Figure 1.


Figure 1

The following code snippet changes the values of CenterX and CenterY.

<Rectangle Width="200" Height="50" Fill="Blue"  Opacity="0.5" Margin="61,27,117,184">
                <RotateTransform CenterX="-50" CenterY="50" Angle="45" />

The new output of looks like Figure 2 where you can see the center point of second rectangle is shifted from its original position.


Figure 2

The code listed in Listing creates a RotateTransform object dynamically and set it as RenderTransform property of a Rectangle. The output looks like Figure .

    Private Sub RotateTransformSample()
        Dim originalRectangle As New Rectangle()
        originalRectangle.Width = 200
        originalRectangle.Height = 50
        originalRectangle.Fill = Brushes.Yellow
        Dim rotatedRectangle As New Rectangle()
        rotatedRectangle.Width = 200
        rotatedRectangle.Height = 50
        rotatedRectangle.Fill = Brushes.Blue
        rotatedRectangle.Opacity = 0.5
        Dim rotateTransform1 As New RotateTransform(45, -50, 50)
        rotatedRectangle.RenderTransform = rotateTransform1
    End Sub


More Articles

© 2020 DotNetHeaven. All rights reserved.