0

I would like to zoom image with mouse in the picturebox. The code below zoom the image but the picture box zoom itself. I want to zoom the image without zooming the picturebox. What can i edit the code below? Thanks

 protected override void OnMouseWheel(MouseEventArgs ea)
    {
        Image img = Image.FromFile("C:/Users/User/Desktop/11.png");
        //  flag = 1;
        // Override OnMouseWheel event, for zooming in/out with the scroll wheel
        if (pictureBox1.Image != null)
        {
            // If the mouse wheel is moved forward (Zoom in)
            if (ea.Delta > 0)
            {
                // Check if the pictureBox dimensions are in range (15 is the minimum and maximum zoom level)
                if ((pictureBox1.Width < (15 * this.Width)) && (pictureBox1.Height < (15 * this.Height)))
                {
                    // Change the size of the picturebox, multiply it by the ZOOMFACTOR
                    pictureBox1.Width = (int)(pictureBox1.Width * 1.25);
                    pictureBox1.Height = (int)(pictureBox1.Height * 1.25);

                    // Formula to move the picturebox, to zoom in the point selected by the mouse cursor
                    pictureBox1.Top = (int)(ea.Y - 1.25 * (ea.Y - pictureBox1.Top));
                    pictureBox1.Left = (int)(ea.X - 1.25 * (ea.X - pictureBox1.Left));
                }
            }
            else
            {
                // Check if the pictureBox dimensions are in range (15 is the minimum and maximum zoom level)
                if ((pictureBox1.Width > (img.Width)) && (pictureBox1.Height > (img.Height)))
                {// Change the size of the picturebox, divide it by the ZOOMFACTOR
                    pictureBox1.Width = (int)(pictureBox1.Width / 1.25);
                    pictureBox1.Height = (int)(pictureBox1.Height / 1.25);

                    // Formula to move the picturebox, to zoom in the point selected by the mouse cursor
                    pictureBox1.Top = (int)(ea.Y - 0.80 * (ea.Y - pictureBox1.Top));
                    pictureBox1.Left = (int)(ea.X - 0.80 * (ea.X - pictureBox1.Left));

                }
            }
        }
derloopkat
  • 6,232
  • 16
  • 38
  • 45
chris
  • 9
  • 2
  • 2
    Apart from changing size of picture box you should be doing sizemode=zoom, then put picturebox inside a fixed size panel so that overflow won't be visible. Also if you do that panel scrollable, user will have a chance to view the rest of the image. – derloopkat Jul 29 '17 at 09:19
  • drag and drop the picturebox into the panel – derloopkat Jul 29 '17 at 09:39
  • derloopkat's comment points to the most common solution. Recommended. - More on zooming see [here](https://stackoverflow.com/questions/45148535/recompute-panel-autoscrollposition-after-zoom/45154242#45154242) - One can also play with the AutoScrollMinsize. – TaW Jul 29 '17 at 09:59
  • After i add the panel, my mouse POINT to zoom feature disappear and can only zoom from top left. – chris Jul 29 '17 at 10:25

0 Answers0