tags:

views:

407

answers:

1

In response to this question about zooming and panning in WPF I made the suggestion to use a ScaleTransform and update the RenderTransform origin in the MouseMove event. This works but I'm not happy with the fact that the panning movement does not quite match the mouse. I can see what the problem with this algorithm is, but the correct implementation continues to elude me.

private void image_MouseMove(object sender, MouseEventArgs e)
{
    if (image.IsMouseCaptured)
    {
        Vector v = start - e.GetPosition(image);
        // this calculates the deltas relative to the original size of the image
        // but does not take into account the transformed size, but transforming the
        // size like image.ActualWidth * scaleTransform.ScaleX does not help
        double deltax = v.X / image.ActualWidth;
        double deltay = v.Y / image.ActualHeight;

        image.RenderTransformOrigin = new Point(orgin.X + deltax, orgin.Y + deltay);
    }
}

Any ideas on how I can make this approach work?

+1  A: 

It's quite strange that it doesn't work for you. I'm using similar way to do the panning. But I do divide the delta by scaleTransform.ScaleX.

The other difference is that I add a TranslateTransform to the TransformGroup and modify X and Y of the translation instead of using RenderTransformOrigin.

arconaut
Thanks for the pointer, I should have used a TraslateTransform from the beginnig, but was trying to do it all using just a ScaleTransform, I have updated the answer to the original question using your suggestion. Thanks.
Ian Oakes