How to zoom an image in&out in C#?

Mehmet Ince picture Mehmet Ince · Jun 6, 2012 · Viewed 56.3k times · Source

I want to implement zoom for an image. I don't want to resize the PictureBox, but the image itself.

How do I do this?

Answer

Thorsten Dittmar picture Thorsten Dittmar · Jun 6, 2012

One solution is:

  1. Create new image of the desired size (for example 200% or 50% of original image size)
  2. Draw original image to new image using Graphics.DrawImage(Image, Rectangle);, which draws the given image to the new image at the given position with the given size
  3. Set new image as source for the PictureBox

Another way is to simple create a new bitmap instance like that:

Size newSize = new Size((int)(originalBitmap.Width * zoomFactor), (int)(originalBitmap.Height * zoomFactor));
Bitmap bmp = new Bitmap(originalBitmap, newSize);