#742 – Using Touch Manipulation Events to Rotate an Element

In addition to using the touch manipulation events to handle translation of an element, we can use the same mechanisms to allow a user to rotate an element using touch.

We can do both translation and rotation in the same event handler.  The ManipulationDelta object gives us both a translation (vector) and a rotation (angle).  Both are automatically incorporated into the ManipulationDelta object, based on how the user is touching the screen.  The user can translate by sliding one finger around and can rotate by placing two fingers on the object and rotating it.

We transform the element by calling two different functions of the underlying Matrix, for both translation and rotation.

Here’s the XAML, containing a single Image control that we’ll interact with.

    <Canvas Name="canvMain" Background="Transparent">
        <Image Source="JamesII.jpg" Width="100"
               RenderTransform="{Binding ImageTransform}"
               ManipulationStarting="Image_ManipulationStarting" ManipulationDelta="Image_ManipulationDelta"/>

Here is the source code, with the updated ManipulationDelta event handler.

    public partial class MainWindow : Window, INotifyPropertyChanged
        public MainWindow()
            this.DataContext = this;

            ImageTransform = new MatrixTransform();

        private MatrixTransform imageTransform;
        public MatrixTransform ImageTransform
            get { return imageTransform; }
                if (value != imageTransform)
                    imageTransform = value;

        private void Image_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
            // Ask for manipulations to be reported relative to the canvas
            e.ManipulationContainer = canvMain;

        private void Image_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
            ManipulationDelta md = e.DeltaManipulation;
            Vector trans = md.Translation;
            double rotate = md.Rotation;

            Matrix m = imageTransform.Matrix;

            // Find center of element and then transform to get current location of center
            FrameworkElement fe = e.Source as FrameworkElement;
            Point center = new Point(fe.ActualWidth / 2, fe.ActualHeight / 2);
            center = m.Transform(center);

            // Update matrix to reflect translation/rotation
            m.Translate(trans.X, trans.Y);
            m.RotateAt(rotate, center.X, center.Y);

            imageTransform.Matrix = m;

            e.Handled = true;

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string prop)
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));



About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

9 Responses to #742 – Using Touch Manipulation Events to Rotate an Element

  1. Pingback: Dew Drop – January 25, 2013 (#1,487) | Alvin Ashcraft's Morning Dew

  2. Jai says:

    Hello there, is this Manipulation events can be apply to mouse? p/s I’m new in this .NET @ WPF things

  3. Jai says:

    Do you have any sample to do better rotation/pan by using mouse or other control?, I have been searching got some of it but not really good a bit jagged…

    • Sean says:

      Well, remember that a mouse will only have one contact point, whereas touch can have many contact points. To rotate an element with touch, you’d use two contact points, so you can detect the points rotating about a center point. This isn’t possible with a mouse because you only have one mouse pointer. Typically, to rotate something using just a mouse, you’d add an additional control, like a slider or something.

  4. vijaylaxmi says:

    i want to rotate an image using only three touch not from two touch ,can u tell me what i have to modify in above code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: