How can I zoom in and zoom out an image programmatically in C#?
I would l开发者_C百科ike to be able to zoom (-/+) an image that is placed under a panel. I would like the zoomed version of the image to be displayed within that panel. How can I achieve this in C#?
In simple words, how can I zoom an image in and out when I click on the image programmatically in C#?
If you use GDI+ then you can certainly do this by using the Matrix transform class in something like the following manner...
// Get hold of your graphics context...
using(Graphics g = this.CreateGraphics())
{
// When drawing we want to apply a scaling of 2,2 (making it bigger!)
Matrix m = new Matrix();
m.Scale(2, 2, MatrixOrder.Append);
g.Transform = m;
// Draw the actual image using the assigned matrix transform
g.DrawImage(...);
}
When you want to zoom in or out you are just altering the scaling values used.
You'll need to use the GDI+ drawing functions (exposed in the .NET Framework as methods of the Graphics
class) to do this. Essentially, you'll be redrawing a zoomed version of the base image into the display panel.
Using something like Graphics.DrawImage
, all you have to do is specify the base image, the source rectangle (the portion of the base image to zoom in on), and the destination rectangle (the dimensions of the new, zoomed image).
There's a tutorial available here that you may find worth your time to check out (source code download link is at the bottom of the page): http://www.vcskicks.com/image-zoom.php
If this has only just whet your appetite for graphics in C#, check out this article for a more comprehensive introduction to its graphics-related capabilities.
Disclaimer: I work for Atalasoft
Our DotImage Photo SDK is free and has a WinForms control that has this and many other features (panning, autozoom, magnifying glass, etc)
精彩评论