0

when my TransformGroup combines ScaleTransform, RotateTransform, and TranslateTransform simultaneously, it is required that the image can be moved, rotated with the center of the image as the rotation center, and scaled with the mouse position as the center. My code movement and rotation are normal, and scaling alone is also normal. However, when I rotate and scale again, the scaling center is no longer the center of the mouse. I think my scaling center calculation is incorrect, May I ask how to calculate the correct position? TransformGroup

RenderTransformGroup = new TransformGroup();
RenderTransformGroup.Children.Add(ScaleTransform);
RenderTransformGroup.Children.Add(RotateTransform);
RenderTransformGroup.Children.Add(TranslateTransform);

rotate operate

RotateTransform.Angle = angle;
RotateTransform.CenterX = (int)(imageWidth * ScaleTransform.ScaleX / 2);
RotateTransform.CenterY = (int)(imageHeight * ScaleTransform.ScaleY / 2);

scale operate MouseWheel

var point= e.GetPosition(Canvas);
var inversePoint = RenderTransformGroup.Inverse.Transform(point);
ScaleTransform.ScaleX += scale;
ScaleTransform.ScaleY += scale;
TranslateTransform.X = point.X - inversePoint.X * ScaleTransform.ScaleX;
TranslateTransform.Y = point.Y - inversePoint.Y * ScaleTransform.ScaleY;

translate operate MouseLeftButtonDown

PreviousPoint = e.GetPosition(image.Parent as Canvas);

MouseMove

CurrentPoint = e.GetPosition(image.Parent as Canvas);
TranslateTransform.X += (CurrentPoint.X - PreviousPoint.X);
TranslateTransform.Y += (CurrentPoint.Y - PreviousPoint.Y);
PreviousPoint = CurrentPoint;

I hope to correctly calculate the zoom center centered around the mouse point when zooming in and out after rotation

  • Better use a single MatrixTransform where you can update the Matrix property with a Matrix that is modified by methods like ScaleAt, RotateAt etc. – Clemens Jul 11 '23 at 05:48
  • Something like this: https://stackoverflow.com/a/22353109/1136211 – Clemens Jul 11 '23 at 05:49
  • @Clemens Thank you for your answer. I noticed another issue with using MatrixTransform. I achieved rotation through a slider of 0-360 °. If I used Matrix.RotateAtPrepend, I found that when the slider returned to its original position, the image was no longer in its original direction – destiny Jul 11 '23 at 06:21

0 Answers0