0

I do have following problem when zooming into an image in a picturebox. After several times zooming in and out, the bmp image gets very unsharp. Does anyone konws how to solve this problem?

Here is the code:

    public Form1()
    {
        InitializeComponent();

        pictureBox1.MouseEnter += new EventHandler(pictureBox1_MouseEnter);
        pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);

        //Set the SizeMode to center the image.
        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
    }


    private double zoom = 1.0;
    void pictureBox1_MouseWheel(object sender, MouseEventArgs e) 
    { 

       if (pictureBox1.Image != null)
       {
           if (e.Delta < 0)
           {
               zoom = zoom * 1.05;
           }
           else
           {
               if (zoom != 1.0)
               {
                   zoom = zoom / 1.05;
               }
           }

           txttextBox1.Text = zoom.ToString();

           Bitmap bmp = new Bitmap(pictureBox1.Image, Convert.ToInt32(pictureBox1.Width * zoom), Convert.ToInt32(pictureBox1.Height * zoom));
           Graphics g = Graphics.FromImage(bmp);
           g.InterpolationMode = InterpolationMode.Default;
           pictureBox1.Image = bmp;
       }

    }  


    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        pictureBox1.Focus();
    }

It does not matter when I change the interpolation mode! Thanks!

Norick
  • 215
  • 1
  • 8
  • 19
  • Possible duplicate: http://stackoverflow.com/questions/30510307/image-in-picturebox-become-blurry-while-rotating/30510375#30510375 – adv12 Jun 15 '15 at 14:10
  • 2
    The basic problem is that you are creating approximations of approximations of the original image. To avoid that, keep the original image around and only ever create scaled versions from that original image. – adv12 Jun 15 '15 at 14:12
  • 1
    Another (possibly better) solution is to set the `SizeMode` of your `PictureBox` to `Zoom` and then resize the PictureBox itself to make the picture the right size, as suggested in some answers here: https://social.msdn.microsoft.com/Forums/en-US/50d2ad98-405e-4943-a300-021fd3ecbaa4/how-to-zoom-in-and-out-in-picture-box?forum=vblanguage – adv12 Jun 15 '15 at 14:19
  • 2
    You can't zoom-in beyond the information that is in the image. Zooming cannot create new and true information. - If you mean that zooming-in and -out repeatedly loses information: yes that is possible, especially when you always create a __new__ bitmap and work from that one. Do not do that!! __Always start from the original image__!! - A picturebox can do it all for you, if you set the sizemode=zoom and increase the PB.Size. Put it inside a Panel to restrict its visible size! Make the panel autoscroll to get scrollbars! – TaW Jun 15 '15 at 15:13
  • @adv12 yes, this is the better method with the SizeMode! Thanks – Norick Jun 16 '15 at 08:52
  • @TaW good idea with the Panel around the PictureBox!!! Thanks – Norick Jun 16 '15 at 08:52

1 Answers1

0

Without a good, minimal, complete code example that clearly shows your scenario, it's difficult if not impossible to know for sure what the best solution is.

However, in general the right way to approach this would be to configure the PictureBox to scale the image according to its own size (i.e. set SizeMode to SizeMode.Zoom), and then resize the PictureBox itself according to the desired zoom factor.

For example:

void pictureBox1_MouseWheel(object sender, MouseEventArgs e) 
{
   if (pictureBox1.Image != null)
   {
       if (e.Delta < 0)
       {
           zoom = zoom * 1.05;
       }
       else
       {
           if (zoom != 1.0)
           {
               zoom = zoom / 1.05;
           }
       }

       txttextBox1.Text = zoom.ToString();

       pictureBox1.Width = (int)Math.Round(pictureBox1.Image.Width * zoom);
       pictureBox1.Height = (int)Math.Round(pictureBox1.Image.Height * zoom);
   }
}  
Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • This works but how can I set the zoom area to the location where the mouse click is? – Norick Jun 16 '15 at 08:59
  • You need to adjust the `PictureBox` location as you zoom as well then. That specific variation on your question appears to have been answered several times: http://stackoverflow.com/a/30415841, http://stackoverflow.com/a/24738203, http://stackoverflow.com/a/18892796 – Peter Duniho Jun 16 '15 at 15:44
  • I allready checked those. The main problem is that a shift of the PictureBox location after zooming shifts the whole PictureBox and therefore I have a space. I rather need to find a solution where I can use something like scrollbars to adjust. – Norick Jun 17 '15 at 06:57