ARTICLE

RotateTransform in WPF

Posted by Mahesh Chand Articles | WPF February 13, 2010
This article demonstrates how to rotate elements and controls in WPF using XAML and C#.
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 void RotateTransformSample()

{

    Rectangle originalRectangle = new Rectangle();

    originalRectangle.Width = 200;

    originalRectangle.Height = 50;

    originalRectangle.Fill = Brushes.Yellow;

    LayoutRoot.Children.Add(originalRectangle);

 

    Rectangle rotatedRectangle = new Rectangle();

    rotatedRectangle.Width = 200;

    rotatedRectangle.Height = 50;

    rotatedRectangle.Fill = Brushes.Blue;

    rotatedRectangle.Opacity = 0.5;

    RotateTransform rotateTransform1 = new RotateTransform(45, -50, 50);

    rotatedRectangle.RenderTransform = rotateTransform1;

 

    LayoutRoot.Children.Add(rotatedRectangle);

}

Login to add your contents and source code to this article
post comment
     

Hello,I hava a book,which is writted by Charles Petzold.It's about WPF 3D.A example is using mouse change a attribute In WPF from C# code so that  cube can move.But it can't rorate.My English is not good.Do you understand it?The code is:
<Window x:Class="rotateTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="900">
  <Page>
    <Page.Resources>
        <MeshGeometry3D x:Key="cube"
                    Positions="-0.5 0.5 0.5,0.5 0.5 0.5,
                               -0.5 -0.5 0.5,0.5 -0.5 0.5,

                               0.5 0.5 -0.5,-0.5 0.5 -0.5,
                               0.5 -0.5 -0.5,-0.5 -0.5 -0.5,

                               -0.5 0.5 -0.5,-0.5 0.5 0.5,
                               -0.5 -0.5 -0.5,-0.5 -0.5 0.5,

                               0.5 0.5 0.5,0.5 0.5 -0.5,
                               0.5 -0.5 0.5,0.5 -0.5 -0.5,
      
                               -0.5 0.5 -0.5,0.5 0.5 -0.5,
                               -0.5 0.5 0.5,0.5 0.5 0.5,

                               0.5 -0.5 -0.5,-0.5 -0.5 -0.5,
                               0.5 -0.5 0.5,-0.5 -0.5 0.5"
                   TriangleIndices="0 2 1,1 2 3
                                    4 6 5,5 6 7
                                    8 10 9,9 10 11
                                    12 14 13,13 14 15
                                    16 18 17,17 18 19
                                    20 22 21,21 22 23"/>
    </Page.Resources>
    <DockPanel>
            <Viewport3D Name="viewport">
                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <GeometryModel3D Geometry="{StaticResource cube}">
                            <GeometryModel3D.Material>
                                <DiffuseMaterial Brush="AntiqueWhite"/>
                            </GeometryModel3D.Material>
                        </GeometryModel3D>
                    </ModelVisual3D.Content>
                    <ModelVisual3D.Transform>
                        <TranslateTransform3D OffsetX="-1"/>

                        <!--<ScaleTransform3D ScaleX="1" ScaleY="0.07" ScaleZ="7"/>
                           
                            <RotateTransform3D>
                                <RotateTransform3D.Rotation>
                                    <AxisAngleRotation3D Axis="0 0 1"/>
                                </RotateTransform3D.Rotation>
                            </RotateTransform3D>
                        </Transform3DGroup>-->
                    </ModelVisual3D.Transform>
                </ModelVisual3D>
                <!--Light Source-->
                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <Model3DGroup>
                            <AmbientLight Color="#808080"/>
                            <DirectionalLight Color="#808080" Direction="2 -3 -1"/>
                        </Model3DGroup>
                    </ModelVisual3D.Content>
            </ModelVisual3D>
           
                <!--Camrea-->
                <Viewport3D.Camera>
                    <PerspectiveCamera Position="2 2 8"
                                   LookDirection="-1.5 -1.5 -6"
                                   UpDirection="0 1 0"
                                   FieldOfView="45"/>
                </Viewport3D.Camera>
            </Viewport3D>
    </DockPanel>
  </Page>
</Window>

c# code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace rotateTest
{
    /// <summary>
    /// Window1.xaml ?????
    /// </summary>
    public partial class Window1 : Window
    {
        static Point a, b;
        static TranslateTransform3D transTracking;
        public Window1()
        {
            InitializeComponent();
            a = new Point();
            b = new Point();
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            a = e.GetPosition(viewport);//??????a
           
        }
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            b = e.GetPosition(viewport);//??????b
            int range = (int)(a.X - b.X);

            //??xaml?????
            HitTestResult result = VisualTreeHelper.HitTest(viewport, a);

            RayMeshGeometry3DHitTestResult result3d = result as RayMeshGeometry3DHitTestResult;
            if (result3d == null)
                return;

            ModelVisual3D vis3d = result3d.VisualHit as ModelVisual3D;
            if (vis3d == null)
                return;
            transTracking = vis3d.Transform as TranslateTransform3D;
            if (transTracking == null)
            {
                MessageBox.Show("null-1");
                return;
            }
            transTracking.OffsetX = range/60;
        }
    }
}

Posted by peng kun Sep 06, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.