ARTICLE

WPF RotateTransform in VB.NET

Posted by Dinesh Beniwal Articles | WPF using VB.NET June 16, 2010
This article demonstrates how to rotate elements and controls in WPF using XAML and VB.NET.
 
Reader Level:

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.

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

The output of Listing looks like Figure 1.

RotateImg1.jpg

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">
            <Rectangle.RenderTransform>
                <RotateTransform CenterX="-50" CenterY="50" Angle="45" />
            </Rectangle.RenderTransform>
</
Rectangle>

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

RotateImg2.jpg

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
        LayoutRoot.Children.Add(originalRectangle)
        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
        LayoutRoot.Children.Add(rotatedRectangle)
    End Sub

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor