Praveen Kumar
Praveen Kumar

Reputation: 1658

How to zoom-in and zoom-out an image in PictureBox using the mouse wheel?

I want to zoom-in or zoom-out an image on PictureBox using the mouse wheel in C#. How can I do it?

Upvotes: 5

Views: 35223

Answers (3)

Cork
Cork

Reputation: 1

You'd probably need to manipulate the image; set the sizemode to stretch; generally instantiate a System.Drawing.Rectangle that's a smaller than the original bitmap (calculate the new height/width/starting point according to your desired zoom level...), then use the Bitmap.Clone(Rect,PixelFormat) to reset the image; i.e.:

Default/Complete Picture:

Dim BM As New Bitmap(MyPicture)

   PBox.Image = BM

Zoomed 10%:

Dim Rect As New System.Drawing.Rectangle(0, 0, BM.Width * .90, BM.Height * .90)

   PBox.Image = BM.Clone(Rect, BM.PixelFormat)

Essentially the "Stretch" should now stretch the smaller image into the picturebox's full dimensions, essentially zooming it... I have no idea how efficient this would be though, or if there isn't a better way to handle it.

Follow-up: I was able to generate a zoom effect using the Clone/Rect logic above, but I should note that I triggered a memory error replacing the Picture.Image without disposing of it first. I also found anomalies re: disposing of the image. Ultimately came up with this code which seems to be working without any errors:

Friend Sub PBoxRefresh()

If PBox.Image IsNot Nothing Then PBox.Image.Dispose()

If ImageRect.Height = MainImage.Height And ImageRect.Width = MainImage.Width Then

  PBox.Image = MainImage.Clone

Else

  PBox.Image = MainImage.Clone(ImageRect, MainImage.PixelFormat)

End If

End Sub

I generally found that if I did PBox.Image = MainImage then when PBox.Image was disposed, MainImage was as well, ergo need to use .Clone.

Also note, someone made a comment about this not being c# - Sorry about that, but I was only trying to address the logic necessary to achieve a zoom effect without resizing the PictureBox and not write the code for them. The code is short enough that it shouldn't be too hard to translate. This also doesn't address using the mouse wheel to handle changing the rectangle size, nor mouse movements to change the rectangle location, but, like I said, seems to work re: handling the actual zooming in the PictureBox. For expediency, I used trackbars for changing size/X/Y in my tests...

Upvotes: -1

Sivodaya Tech
Sivodaya Tech

Reputation: 128

This topic helps to zoom in and out picture in picturebox

add below code inside the picturebox mouse wheel event

if (e.Delta != 0) {
    if (e.Delta <= 0) {
        //set minimum size to zoom
        if (PictureBox1.Width < 50)
            return;
    } else {
        //set maximum size to zoom
        if (PictureBox1.Width > 500)
            return;
    }
    PictureBox1.Width += Convert.ToInt32(PictureBox1.Width * e.Delta / 1000);
    PictureBox1.Height += Convert.ToInt32(PictureBox1.Height * e.Delta / 1000);
}

Upvotes: 3

Related Questions