0

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?

EKOlog
  • 410
  • 7
  • 19
Jodynek
  • 103
  • 2
  • 10
  • What do you think what's wrong with this? What is the true question here. You think that it performs below expectation? – Jeroen van Langen Oct 22 '21 at 22:19
  • I am attempting to develop a custom control that allows the user to zoom to a specific point on an image (where mouse pointer is located) - like in any map, for example. So I also need to calculate the position of AutoScrollPosition in the Panel. – Jodynek Oct 22 '21 at 22:25
  • You'll probably need the `e.Graphics.TranslateTransform(` also. Maybee something like: `transform(-x, -y); Scale(,); Transform(x,y);` to move the 'center point of scaling'. – Jeroen van Langen Oct 22 '21 at 22:36
  • 2
    [Zoom and translate an Image from the mouse location](https://stackoverflow.com/questions/61924051/zoom-and-translate-an-image-from-the-mouse-location). See also the relevant methods [here](https://stackoverflow.com/a/56362612/14171304) & [here](https://stackoverflow.com/a/39305038/14171304). – dr.null Oct 23 '21 at 01:38

0 Answers0