Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can u tell me how to zoom in and zoom out image in windows form. i have a picture box which display image. now i dont know how to write code to zoom in and zoom out image in picture box. can u help me, please. thanks
Posted

The easiest way is not to use a picture box at all: use a Panel, and paint the image yourself by handling the Paint event foe the panel:
private void panImage_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    g.ScaleTransform(sx, sy);
    g.DrawImage(myImage, new Point(0, 0));
    }
private void butZoomIn_Click(object sender, EventArgs e)
    {
    sx *= 2;
    sy *= 2;
    panImage.Invalidate();
    }
private void butZoomOut_Click(object sender, EventArgs e)
    {
    sx /= 2;
    sy /= 2;
   panImage.Invalidate();
    }
 
Share this answer
 
Comments
Rudra Gouda 28-Jun-11 2:49am    
the second one i should write on clicking zoom in button and the third one i should write on clicking zoom out button but wat about the first code wer should i write that
OriginalGriff 28-Jun-11 4:06am    
"paint the image yourself by handling the Paint event for the panel"
Christian Graus 28-Jun-11 4:04am    
The first one is your paint event. It needs to handle the event on the form. You should buy a book on programming and read it. If the army is going to use your code for anything other than planning a golf course, you should admit to them that you have no idea about programming and certainly should not be getting paid for your work.
Chances are, you don't need to use picture box, see this: How do I clear a panel from old drawing[^].

What you need is to re-sample the bitmap. Too bad you did not tag your question: is it WPF or System.Drawing or something else, but as you mention forms and picture box, I guess you mean Forms. (You need to tag you UI/Graphics library anyway!)

This will explain how to do it with System.Drawing: http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing[^].

—SA
 
Share this answer
 
Wow. Please do not ask the same question over and over. The code you've been shown, is doing exactly what I told you to do. If you'd asked for more detail and clarified, then that would have been the way to go. Of course, this code will lose quality as you zoom, as is inevitable. So, you ARE using images you have locally ?
 
Share this answer
 
Comments
Rudra Gouda 28-Jun-11 2:55am    
yeah i'm using local images from my hard disk

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900