Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » WPF » RotateTransform in WPF

RotateTransform in WPF

This article demonstrates how to rotate elements and controls in WPF using XAML and C#.

Author Rank :
Page Views : 13621
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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);

}

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
I want to rorate a cube in 3D,use my mouse by peng On September 6, 2010

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;
        }
    }
}

Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.