question

JamesGrice-3158 avatar image
0 Votes"
JamesGrice-3158 asked JamesGrice-3158 commented

Image Function Rotate and zoom

hey guys!

So i am currently adding a mapviewing component to my application which uses a picture box and this code i found from here: rotate-a-picture-in-c
I would like to change up this function to allow the user to zoom to a point probably like :
private void DisplayImage(Point location, bool zoomIn)

with location being the location within the picturebox where the user will be zooming from.
I tried messing arround with gr.ScaleTransform with no success.

any help would be greatly appreciated!



 // Display the image at the current rotation and scale.
 private void DisplayImage()
 {
     if (OriginalImage == null) return;
     RotatedImage = null;
     picRotatedImage.Visible = false;
    
     float angle;
     if (!float.TryParse(txtAngle.Text, out angle)) return;
    
     // Find the size of the image's rotated bounding box.
     Matrix rotation = new Matrix();
     rotation.Rotate(angle);
     int old_wid = OriginalImage.Width;
     int old_hgt = OriginalImage.Height;
     PointF[] points =
     {
         new PointF(0, 0),
         new PointF(old_wid, 0),
         new PointF(0, old_hgt),
         new PointF(old_wid, old_hgt),
     };
     rotation.TransformPoints(points);
     float[] xs =
         { points[0].X, points[1].X, points[2].X, points[3].X };
     float[] ys =
         { points[0].Y, points[1].Y, points[2].Y, points[3].Y };
     int new_wid = (int)(xs.Max() - xs.Min());
     int new_hgt = (int)(ys.Max() - ys.Min());
    
     // Make the rotated image.
     RotatedImage = new Bitmap(new_wid, new_hgt);
     using (Graphics gr = Graphics.FromImage(RotatedImage))
     {
         gr.TranslateTransform(-old_wid / 2, -old_hgt / 2,
             MatrixOrder.Append);
         gr.RotateTransform(angle, MatrixOrder.Append);
         gr.TranslateTransform(new_wid / 2, new_hgt / 2,
             MatrixOrder.Append);
         RectangleF source_rect = new RectangleF(0, 0,
             OriginalImage.Width, OriginalImage.Height);
         PointF[] dest_points =
         {
             new PointF(0, 0),
             new PointF(OriginalImage.Width, 0),
             new PointF(0, OriginalImage.Height),
         };
         gr.DrawImage(OriginalImage, dest_points, source_rect,
             GraphicsUnit.Pixel);
     }
    
     // Scale the output PictureBox.
     SetPictureBoxSize();
    
     // Display the result.
     picRotatedImage.Image = RotatedImage;
     picRotatedImage.Visible = true;
 }




dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JamesGrice-3158,
>>I would like to change up this function to allow the user to zoom to a point probably like :
The zoom mode of pictureBox uses x and y scale factors to zoom the picture instead of location, the same is true for the Graphics.ScaleTransform method.
In the link you provided, you set the picture box to display the image at the required scale.
And here is a related thread you can refer to.
Best Regards,
Daniel Zhang


0 Votes 0 ·

1 Answer

Castorix31 avatar image
1 Vote"
Castorix31 answered JamesGrice-3158 commented

A way =>

109543-zoompicture.gif

 public partial class Form1 : Form
 {
     public Form1()
     {
         //InitializeComponent();
         this.Load += new System.EventHandler(this.Form1_Load);
     }
    
     private System.Windows.Forms.PictureBox pictureBox1;
     private System.Windows.Forms.PictureBox pictureBox2;
     private Point ptPicture1;
     int nWidth, nHeight = 0;
     float nScaleX, nScaleY = 0;
     bool bCompleted = false;  
    
     private void Form1_Load(object sender, EventArgs e)
     {
         pictureBox1 = new System.Windows.Forms.PictureBox();          
         pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;            
         pictureBox1.Size = new System.Drawing.Size(1920 / 5, 1200 / 5);
         pictureBox1.Location = new System.Drawing.Point(100, 20);
    
         pictureBox2 = new System.Windows.Forms.PictureBox();
         pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         pictureBox2.Size = new System.Drawing.Size(1920 / 5, 1200 / 5);
         pictureBox2.Location = new System.Drawing.Point(100, 300);
    
         ClientSize = new System.Drawing.Size(600, 560);
         Controls.Add(pictureBox1);
         Controls.Add(pictureBox2);
    
         CenterToScreen();
    
         pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
         pictureBox1.WaitOnLoad = false;           
         pictureBox1.LoadAsync(@"https://i.ibb.co/hZQWXMX/waterfall-1624701760844-2505.jpg");
         pictureBox1.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this.pictureBox1_LoadCompleted);
         pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
         pictureBox2.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox2_Paint);
     }
    
     private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
     {
         ptPicture1 = e.Location;
         pictureBox2.Invalidate();
     }
    
     private void pictureBox2_Paint(object sender, PaintEventArgs e)
     {
         if (bCompleted)
         {
             float nScaleDest = (float)pictureBox2.Width / (float)pictureBox2.Height;
             int nSourceWidth = 200; // Zoom 
             int nSourceHeight = (int)(nSourceWidth / nScaleDest);
             Rectangle rectSource = new Rectangle((int)(ptPicture1.X * nScaleX - nSourceWidth / 2), (int)(ptPicture1.Y * nScaleY - nSourceHeight / 2), nSourceWidth, nSourceHeight);
             float nFactor = pictureBox2.Width / (float)nSourceWidth;
             Rectangle rectDest = new Rectangle(0, 0, (int)(nSourceWidth * nFactor), (int)(nSourceWidth * nFactor * ((float)nHeight / (float)nWidth)));
             e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
             e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
             e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             e.Graphics.DrawImage(pictureBox1.Image, rectDest, rectSource, GraphicsUnit.Pixel);
         }
     }
    
     private void pictureBox1_LoadCompleted(Object sender, AsyncCompletedEventArgs e)
     {
         nWidth = pictureBox1.Image.Width;
         nHeight = pictureBox1.Image.Height;
         nScaleX = nWidth / pictureBox1.ClientSize.Width;
         nScaleY = nHeight / pictureBox1.ClientSize.Height;
         bCompleted = true;
     }  
 }




zoompicture.gif (2.6 MiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Awesome thank you!! :D

0 Votes 0 ·