Q: How can I implement zooming on current mouse position over a picturebox, something like zooming in Google Maps?
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.
Maps are standard JPEG or PNG format digital images and most of them have very high resolutions (2000x2000px and up).
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.
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.
The code for zooming in currently looks like this:
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));
- The default "zoomRatio" value is 1, and it is being increased up to 0.6f.
- Argument "timesZoomed" default value is 0, it goes up to 6.
- "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.
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:
pbxMapa.Location = new Point(pbxMapa.Location.X + (int)((pbxMapa.Location.X * zoomRatio - mouseXPbx) / 8), pbxMapa.Location.Y + (int)((pbxMapa.Location.Y * zoomRatio - mouseYPbx) / 8));
- "mouseXPbx" and "mouseYPbx" variables represent the current mouse position inside of the "pbxMapa". I divided by 8 for minimizing the offset in the positioning.
Any help and suggestions is appreciated, thanks in advance.