简体   繁体   English

为什么当我用鼠标拖动pictureBox中的图像时,它将图像恢复为原始大小?

[英]Why when i drag with the mouse the image in the pictureBox it return the image to its original size?

In my code im using the mouse wheel to resize the image in the pictureBox. 在我的代码中,im使用鼠标滚轮来调整pictureBox中的图像大小。

void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            CalculateNewSizeFactor(e.Delta);
            if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
            pictureBox1.Image = null;
            pictureBox1.Image = ResizeImage(new Size((int)(img.Width * factor), (int)(img.Height * factor)));
        }

        public Image ResizeImage(Size size)
        {
            return new Bitmap(img, size);
        }

        private void CalculateNewSizeFactor(int delta)
        {
            if (delta > 0 && factor < 2)
            {
                factor *= increment;
            }
            else if (delta < 0 && factor > 0.25)
            {
                factor /= increment;
            }
        }

Now i added a new checkbox so if its checked i can drag the image around inside the pictureBox: 现在,我添加了一个新复选框,如果选中该复选框,则可以在pictureBox内拖动图像:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                panning = true;
            }
        }

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (checkBox1.Checked)
            {
                panning = true;
            }
            startingPoint = new Point(e.Location.X - movingPoint.X,
                                      e.Location.Y - movingPoint.Y);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            panning = false;
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (panning)
            {
                e.Graphics.Clear(Color.White);
                e.Graphics.DrawImage(img, movingPoint);
            }
        }

There are some problems: 有一些问题:

  1. When i check the checkbox once i move the mouse inside the pictureBox1 area it capture and drag the image and i want it will do it only when i make left click on the mouse. 当我将复选框移动到pictureBox1区域内时,选中该复选框即可捕获并拖动图像,并且我希望它仅在单击鼠标左键时才能执行。

  2. If first i use the mouse wheel and make the image size smaller or bigger and then try to drag it around once i click the image to drag it(mouse down) or when i leave it(mouse up) the image change its size back to its ogirinal size. 如果首先使用鼠标滚轮,使图像尺寸变大或变大,然后单击图像以将其拖动(向下移动),或者当我离开它(向上移动)时,尝试将其周围拖动,图像将其尺寸更改回它的原始尺寸。 I want it to be kept on the size i changed it with the mouse wheel when i drag it. 我希望它保持在我拖动鼠标时用鼠标滚轮更改的尺寸。

How can i solve this two problems ? 我该如何解决这两个问题?

  1. You set panning mode when you check your checkbox - just get rid of that handler - and that`s all. 当您选中复选框时,您可以设置平移模式-只需摆脱该处理程序-就可以了。 Because you set panning mode as well in your mouseDown handler - based on checkBox state. 因为您还需要在mouseDown处理程序中设置平移模式-基于checkBox状态。

  2. When you paint you picture box you are drawing img, which is (probably) is your original image. 绘制图片框时,您正在绘制img,(可能)它是原始图像。 So just save your resized image somewhere and repaint it. 因此,只需将调整大小后的图像保存在某个地方并重新绘制即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PictureBox中调整图像的大小,直到达到原始图像的大小为止 - Resize an image in a PictureBox until it is the size of the original image 拖放时拖动PictureBox-图片的大小 - Drag and Drop move PictureBox- size of image when I'm dragging 获取真实的鼠标在图像的实际大小中的位置,而不是PictureBox中鼠标的位置 - Getting the real Location of mouse in actual size of image, not the location of mouse in PictureBox 如何通过鼠标拖动图像 - How to drag an image by mouse 为什么在粘贴到 Excel 时图像显示的大小大于其实际大小,我如何才能使其以自然大小显示? - Why does an image display at greater that its actual size when pasting into Excel, and how can I get it to display at its natural size? 从PictureBox保存原始图像,不调整大小 - Save original image from PictureBox, not resized 如果我替换PictureBox控件中的图像,我应该首先处理原始图像吗? .Net Winforms - If I replace an image in a PictureBox control, should I dispose the original image first? .Net Winforms 使用鼠标事件在图片框内移动图像 - Move Image inside the Picturebox using mouse events 鼠标单击图片框时图像的着色区域 - Coloring area of an image on mouse click over picturebox 如何使用鼠标事件在图片框中移动图像 - How to move an image in picturebox using mouse events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM