Rotate image math (C#)

It depends on which point you want to use as a “center” for your rotation. Let’s call the point to the up and left pointA and the one to the right and below pointB. If you want to rotate around the point A so that point B aligns with it, calculating the rotation angle in radians would go like this:

double angle = Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);

I don’t how you’re handling your image, so the following applies only if you’re using System.Drawing.Graphics:

myImage.TranslateTransform(-pointA.X, -pointA.Y);
myImage.RotateTransform((float) angle, MatrixOrder.Append);
myImage.TranslateTransform(pointA.X, pointA.Y, MatrixOrder.Append);

Hope it helps.

Leave a Comment