Zooming to point in PictureBox

348 Views Asked by At

I have PictureBox which is inside Panel. Panel has set property AutoScroll == true, PictureBox has Sizemode == Zoom. How to simply implement zoom to point in this code?

public partial class frmMain : Form
{
    string imageFilename = @"c:\Temp\test.jpg";
    private Image _currentImage;
    private float _zoom = 1.0f;
    private Point _currMousePos;

    public frmMain()
    {
        InitializeComponent();

        RefreshImage(imageFilename);
        pictureBox.MouseWheel += PictureBox_MouseWheel;
    }

    private void RefreshImage(string fileName)
    {
        _currentImage = Image.FromFile(fileName);
        pictureBox.Invalidate();
    }

    private void PictureBox_MouseWheel(object sender, MouseEventArgs e)
    {
        if (e.Delta > 0)
        {
            _zoom += 0.1F;
        }
        else if (e.Delta < 0)
        {
            _zoom = Math.Max(_zoom - 0.1F, 0.01F);
        }

        MouseEventArgs mouse = e as MouseEventArgs;
        _currMousePos = mouse.Location;

        pictureBox.Invalidate();

        // how to calculate Panel.AutoScrollPosition? 
    }

    private void pictureBox_Paint(object sender, PaintEventArgs e)
    {
        ((Bitmap)_currentImage).SetResolution(e.Graphics.DpiX, e.Graphics.DpiY);
        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        e.Graphics.ScaleTransform(_zoom, _zoom);
        e.Graphics.DrawImage(_currentImage, 0, 0);
        pictureBox.Size = new Size((int)(float)(_currentImage.Width * _zoom),
            (int)(float)(_currentImage.Height * _zoom));
    }
}

Or should I choose a completely different way of zooming?

0

There are 0 best solutions below