0
votes

I'm trying to properly Zoom in/out Image in PictureBox. I have this code.

private Image GridMap;
private double ZoomFactor = 1;
protected override void OnMouseWheel(MouseEventArgs e)
{
    if (e.Delta > 0)
    {
        ZoomFactor*=1.2;
    }

    else if (e.Delta < 0 && ZoomFactor >1 )
    {
        ZoomFactor /= 1.2;
    }

    Size newSize = new Size((int)(GridMap.Width * ZoomFactor), (int)(GridMap.Height * ZoomFactor));
    Bitmap bmp = new Bitmap(GridMap, newSize);
    MainGrid.Image = bmp;
}

Where MainGrid is PictureBox where i want to zoom.

This code works, but very slow after scrolling I wait 1-2seconds and then it shows the zoomed picture. with (800,800) image. Which is very slow.

I think I know why. Its copying resized bitmap instead of using just part of old one, but I don't know how to do it.

How Can I make it smoothly zooming?

1

1 Answers

0
votes

Ok At the end i figure that out... What i needed is to cut out piece of my bitmap final code is>

protected override void OnMouseWheel(MouseEventArgs e)
    {

        if (e.Delta > 0 && ZoomFactor >MaxZoom)
        {
            ZoomFactor-=0.01;
        }

        else if (e.Delta < 0 && ZoomFactor <1 )
        {
            ZoomFactor += 0.01;
        }

        Rectangle srcRect = new Rectangle(0, 0, (int)(GridMap.Width * ZoomFactor), (int)(GridMap.Height * ZoomFactor));
        Bitmap cropped = ((Bitmap)GridMap).Clone(srcRect, MainGrid.Image.PixelFormat);
        MainGrid.Image = cropped;
    }

And plus Initiate of PictureBox with

this.MainGrid.SizeMode = PictureBoxSizeMode.Zoom;