title: Zooming / stretching a picturebox on current mouse position?
body: <p><strong>Q: How can I implement zooming on current mouse position over a picturebox, something like zooming in Google Maps?</strong> </p>
  
  <p>I am designing a simple GIS / map engine as my thesis work. The application is designed in such a way that the maps are being loaded into tabs of a sligtly modified tabcontrol. </p>
  
  <p>Maps are standard JPEG or PNG format digital images and most of them have very high resolutions (2000x2000px and up).</p>
  
  <p>They are loaded in pictureboxes that are added as subcontrols of tabpages. I've implemented a simple zooming method as a button click event that only zooms to the center of the image/picturebox.</p>
  
  <p>What i would like to do is implement the zooming on a mousewheel event in that way that the picture is being zoomed on the current mouse position inside of the picturebox.</p>
  
  <p>The code for zooming in currently looks like this:</p>
  
  <pre><code>            timesZoomed += 1;
              zoomRatio += 0.1f;
              pbxMapa.Width = pbxMapa.Width * zoomRatio;
              pbxMapa.Height = pbxMapa.Height * zoomRatio;
              pbxMapa.Location = new Point((this.Width / 2) - (pbxMapa.Width / 2), this.Height / 2) - (pbxMapa.Height / 2));
  </code></pre>
  
  <ul>
  <li>The default "zoomRatio" value is 1, and it is being increased up to 0.6f.</li>
  <li>Argument "timesZoomed" default value is 0, it goes up to 6.</li>
  <li>"pbxMapa" is the picturebox with the loaded image of the map. "ImageSizeMode" prop of the picturebox is set to "Zoom", but the size of the picturebox is set to the full size of the loaded map image.</li>
  </ul>
  
  <p>Also, i was experimenting with this simple zooming code. The calculation is somewhat effective, but still it has quite an offset when zooming/multiplying with a bigger ratio:</p>
  
  <pre><code>                pbxMapa.Location = new Point(pbxMapa.Location.X + (int)((pbxMapa.Location.X * zoomRatio - mouseXPbx) / 8), pbxMapa.Location.Y + (int)((pbxMapa.Location.Y * zoomRatio - mouseYPbx) / 8));
  </code></pre>
  
  <ul>
  <li>"mouseXPbx" and "mouseYPbx" variables represent the current mouse position inside of the "pbxMapa". I divided by 8 for minimizing the offset in the positioning.</li>
  </ul>
  
  <p>Any help and suggestions is appreciated, thanks in advance.</p>