首页 > 解决方案 > 缩放到 PictureBox 中的点

问题描述

我有在 Panel 内的 PictureBox。Panel 有设置属性 AutoScroll == true,PictureBox 有 Sizemode == Zoom。如何简单地实现缩放以指向此代码?

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));
    }
}

还是我应该选择一种完全不同的缩放方式?

标签: c#gdidrawing2d

解决方案


推荐阅读