title: Zoom an image in a picture box
body: <p>I know there are many discussion this topic already but still i could not find an answer. Hope someone have a idea.</p>
  
  <p>I load a bitmap in a <code>PictureBox</code> (<code>SizeMode</code> Normal, <code>DockStyle</code> Fill) and use the <code>Paint</code> event to draw the image.</p>
  
  <p>To zoom I use <code>MouseWheel</code> event and increase / decrease zoom factor (Like _zoomFac += 1). The goal is to zoom at the mouse position. So in the <code>MouseWheel</code> event I also save the mouse position (<code>_imageZoomLocation</code>). This is my code (only little test implementation):</p>
  
  <pre><code>public partial class Form1 : Form
  {
    private Image _image;
    private float _zoomFac;
    private PointF _imageZoomLocation;
  
    public Form1()
    {
      InitializeComponent();
      _image = null;
      _zoomFac = 1F;
      _imageZoomLocation = PointF.Empty;
      pictureBox.MouseWheel += new MouseEventHandler(OnMouseWheel);
    }
  
    private void pb_Paint(object sender, PaintEventArgs e)
    {
      if (_image == null)
        return;
      e.Graphics.TranslateTransform(-_imageZoomLocation.X +    _imageZoomLocation.X / _zoomFac, -_imageZoomLocation.Y +   _imageZoomLocation.Y / _zoomFac);
      e.Graphics.ScaleTransform(_zoomFac, _zoomFac, MatrixOrder.Append);
      e.Graphics.DrawImage(_image, new Point(0,0));
    }
  
    private void fileToolStripMenuItem_Click(object sender, EventArgs e)
    {
      ... Code for loading image
    }
  
    private void OnMouseWheel(object sender, MouseEventArgs e)
    {
      if (_image == null)
        return;
  
      _imageZoomLocation = e.Location;
  
      if (e.Delta &gt; 0)
        _zoomFac += 1F;
      else
        if (_zoomFac - 1F &lt; 1F)
          _zoomFac = 1F;
        else
          _zoomFac -= 1F;
  
      Refresh();
    }
  
    private void pb_MouseEnter(object sender, EventArgs e)
    {
      pictureBox.Focus();
    }
  }
  </code></pre>
  
  <p>This works well as long as <code>_imageZoomLocation</code> is not changed, means the mouse is not moved i can zoom in and out perfectly (I can change mouse position when zoom factor is 1 and everything still works). However when I move the mouse to different position in a zoomed state (e.g. zoom factor is 2) and scroll, the image jumpes once to a different position than the mouse is and after this "jump" everything works well again. </p>
  
  <p>Can anyone explain this behaviour?</p>