C#缩放图像而不缩放图片框

时间:2017-07-29 09:11:30

标签: c# winforms picturebox

我想用图片框中的鼠标缩放图像。下面的代码放大图像,但图片框自动缩放。我想缩放图像而不缩放图片框。我可以编辑下面的代码?谢谢

 protected override void OnMouseWheel(MouseEventArgs ea)
    {
        Image img = Image.FromFile("C:/Users/User/Desktop/11.png");
        //  flag = 1;
        // Override OnMouseWheel event, for zooming in/out with the scroll wheel
        if (pictureBox1.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 ((pictureBox1.Width < (15 * this.Width)) && (pictureBox1.Height < (15 * this.Height)))
                {
                    // Change the size of the picturebox, multiply it by the ZOOMFACTOR
                    pictureBox1.Width = (int)(pictureBox1.Width * 1.25);
                    pictureBox1.Height = (int)(pictureBox1.Height * 1.25);

                    // Formula to move the picturebox, to zoom in the point selected by the mouse cursor
                    pictureBox1.Top = (int)(ea.Y - 1.25 * (ea.Y - pictureBox1.Top));
                    pictureBox1.Left = (int)(ea.X - 1.25 * (ea.X - pictureBox1.Left));
                }
            }
            else
            {
                // Check if the pictureBox dimensions are in range (15 is the minimum and maximum zoom level)
                if ((pictureBox1.Width > (img.Width)) && (pictureBox1.Height > (img.Height)))
                {// Change the size of the picturebox, divide it by the ZOOMFACTOR
                    pictureBox1.Width = (int)(pictureBox1.Width / 1.25);
                    pictureBox1.Height = (int)(pictureBox1.Height / 1.25);

                    // Formula to move the picturebox, to zoom in the point selected by the mouse cursor
                    pictureBox1.Top = (int)(ea.Y - 0.80 * (ea.Y - pictureBox1.Top));
                    pictureBox1.Left = (int)(ea.X - 0.80 * (ea.X - pictureBox1.Left));

                }
            }
        }

0 个答案:

没有答案