【问题标题】:How to zoom in using Mouse Position on that image如何在该图像上使用鼠标位置放大
【发布时间】:2020-04-24 10:14:58
【问题描述】:

我有一个 userControl 库,它由主 Panel 和 PictureBox 组成,我想做一个可缩放的 PictureBox 工具,我使用主 Panel 的 mouseWheel 事件放大和缩小,我想不通的问题如何通过鼠标在图像上的位置进行放大,所以每当我放大时,缩放都会转到面板的左上角,那么我该如何解决呢?

private double ZOOMFACTOR = 1.15;   // = 15% smaller or larger
private int MINMAX = 5;
void picPanel_MouseWheel(object sender, MouseEventArgs e)
    {
        if (e.Delta > 0)
        {
            ZoomIn();
        }
        else
        {
            ZoomOut();
        }
    }

    private void ZoomIn()
    {
        if ((picBox.Width < (MINMAX * this.Width)) &&
            (picBox.Height < (MINMAX * this.Height)))
        {
            picBox.Width = Convert.ToInt32(picBox.Width * ZOOMFACTOR);
            picBox.Height = Convert.ToInt32(picBox.Height * ZOOMFACTOR);
        }
    } 
    private void picBox_MouseEnter(object sender, EventArgs e)
    {
        if (picBox.Focused) return;
        picBox.Focus();
    }

更新:

我已经尝试过了,它看起来可以工作,但不完全是应该的!有什么想法吗?

    private void ZoomIn()
    {
        if ((picBox.Width < (MINMAX * this.Width)) &&
            (picBox.Height < (MINMAX * this.Height)))
        {
            picBox.Width = Convert.ToInt32(picBox.Width * ZOOMFACTOR);
            picBox.Height = Convert.ToInt32(picBox.Height * ZOOMFACTOR);

            Point p = this.AutoScrollPosition;
            int deltaX = e.X - p.X;
            int deltaY = e.Y - p.Y;
            this.AutoScrollPosition = new Point(deltaX, deltaY);
        }
    } 

【问题讨论】:

    标签: c#


    【解决方案1】:

    这是在鼠标位置缩放图像的示例.... 测试验证。

    protected override void OnMouseWheel(MouseEventArgs ea)
    {
        //  flag = 1;
        // Override OnMouseWheel event, for zooming in/out with the scroll wheel
        if (picmap1.Image != null)
        {
            // If the mouse wheel is moved forward (Zoom in)
            if (ea.Delta > 0)
            {
                // Check if the pictureBox dimensions are in range (15 is the minimum and maximum zoom level)
                if ((picmap1.Width < (15 * this.Width)) && (picmap1.Height < (15 * this.Height)))
                {
                    // Change the size of the picturebox, multiply it by the ZOOMFACTOR
                    picmap1.Width = (int)(picmap1.Width * 1.25);
                    picmap1.Height = (int)(picmap1.Height * 1.25);
    
                    // Formula to move the picturebox, to zoom in the point selected by the mouse cursor
                    picmap1.Top = (int)(ea.Y - 1.25 * (ea.Y - picmap1.Top));
                    picmap1.Left = (int)(ea.X - 1.25 * (ea.X - picmap1.Left));
                }
            }
            else
            {
                // Check if the pictureBox dimensions are in range (15 is the minimum and maximum zoom level)
                if ((picmap1.Width > (imagemappan.Width)) && (picmap1.Height > (imagemappan.Height)))
                {
                    // Change the size of the picturebox, divide it by the ZOOMFACTOR
                    picmap1.Width = (int)(picmap1.Width / 1.25);
                    picmap1.Height = (int)(picmap1.Height / 1.25);
    
                    // Formula to move the picturebox, to zoom in the point selected by the mouse cursor
                    picmap1.Top = (int)(ea.Y - 0.80 * (ea.Y - picmap1.Top));
                    picmap1.Left = (int)(ea.X - 0.80 * (ea.X - picmap1.Left));
                }
            }
        }
    }
    

    【讨论】:

    • 这个答案有很多额外的内容,但这是我正在寻找的数学 picmap1.Top = (int)(ea.Y - 1.25 * (ea.Y - picmap1.Top)) ;所以谢谢你
    【解决方案2】:

    问题是你的控件就像一个视口——原点在左上角,所以每次你从那个角落拉伸图像时——结果就是你最终放大到左上角,您需要偏移拉伸的图像并将用户放大的点居中。

    • 图像大小:200,200
    • 用户点击 100,50 并放大 x2
    • 拉伸图像
    • 图像大小为 400,400,用户点击的位置现在有效为 200,100
    • 您需要将图像向左滑动 100 像素并向上滑动 50 像素以更正调整图像大小

    您需要覆盖paint 事件处理程序来绘制图像偏移:

     RectangleF BmpRect = new RectangleF((float)(Offset.X), (float)(Offset.Y), (float)(ZoomedWidth), (float)(ZoomedHeight));
      e.Graphics.DrawImage(Bmp, ViewPort , BmpRect, GraphicsUnit.Pixel);
    

    Bmp 是你的形象; ViewPort 是一个由您的图片框控件定义的 Rectangle

    这里是a thread,可能会有所帮助。

    【讨论】:

    • 我已经更新了问题,你能帮我解答吗?
    • 对不起,我还不清楚,我更愿意以我的方式使用它而不覆盖 picBox 的绘制事件,因为我需要使用主面板中的 Scroll,请如果您更新答案,请通知我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多