Here is a static class that will draw the image in the right place within the desired area. Change the rotation value to rotate the image. You can also pan and zoom the image.
Add this class to your project and call static functions from Win Form.
public static class FullImage { public static Image image; public static RectangleF DisplayRect, SourceRect; public static Size ParentBoundry; public static float rotationangle=0; internal static void Paint(Graphics graphics) { if (image == null) return; float hw = DisplayRect.X + DisplayRect.Width / 2f; float hh = DisplayRect.Y + DisplayRect.Height / 2f; System.Drawing.Drawing2D.Matrix m = graphics.Transform; m.RotateAt(rotationangle, new PointF(hw, hh), System.Drawing.Drawing2D.MatrixOrder.Append); graphics.Transform = m; graphics.DrawImage(image, new RectangleF(DisplayRect.X, DisplayRect.Y, DisplayRect.Width, DisplayRect.Height), SourceRect, GraphicsUnit.Pixel); graphics.ResetTransform(); } public static void LoadImage(Image img) { image = img; SizeF s = GetResizedSize(image, ParentBoundry); SourceRect = new RectangleF(0, 0, image.Width, image.Height); DisplayRect = new RectangleF(ParentBoundry.Width / 2 - s.Width / 2, ParentBoundry.Height / 2 - s.Height / 2, s.Width, s.Height); } public static Size GetResizedSize(Image ImageToResize, Size size) { int sourceWidth = ImageToResize.Width; int sourceHeight = ImageToResize.Height; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; nPercentW = ((float)size.Width / (float)sourceWidth); nPercentH = ((float)size.Height / (float)sourceHeight); if (nPercentH < nPercentW) nPercent = nPercentH; else nPercent = nPercentW; int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); return new Size(destWidth, destHeight); } internal static void MouseWheel(int delta) { if (delta > 0) DisplayRect = ZoomImage(DisplayRect,CurrentMouse, .1f); else DisplayRect = ZoomImage(DisplayRect, CurrentMouse, -.1f); } private RectangleF ZoomImage(RectangleF ImageRectangle, PointF MouseLocation, float ScaleFactor) {
After adding this code to the project (it is better to add a separate cs file), call the functions from the Win Form class (Form1.cs).
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); FullImage.ParentBoundry = new Size(this.Width, this.Height);
Now, if you want to rotate the image, just set the desired value (using the slider, button or add a few more functions to detect mouse movement, and then rotate)
Example: add a button and each time the pressed button increases the value by 1.
private void button1_clicked(object sender, EventArgs e) { FullImage.rotationangle++; this.invalidate(); }