简体   繁体   English

在图片框内移动图像

[英]move an image inside a picturebox

I used the following code to move the image inside the image box, but it may come out of the frame when crawling with the mouse right or left.我使用以下代码将图像移动到图像框内,但是当鼠标向右或向左爬行时,它可能会出框。 I want the image not to leave the frame of the image.我希望图像不要离开图像的框架。

my code The image is moving but is outside the frame designated for the image.我的代码 图像正在移动,但位于为图像指定的框架之外。

   private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
          
                if (pictureBox1.Image != null)
                {
                    if ((e.Button == 0))
                    {
                        

                    }
                    else
                    {

                        pictureBox1.Left = (pictureBox1.Left
                              + (e.X - mousePosX));
                        pictureBox1.Top = (pictureBox1.Top
                                    + (e.Y - mousePosY));
                 
                }

                //}


            }
        }

What I want is to move the image.我想要的是移动图像。 And the picture does not come out of the picture box and it keeps moving inside the picture box frame and does not come out of it as shown in the picture..而且图片出不来画框,一直在画框框内移动,如图所示出不来。。

在此处输入图片说明

If I get you right, you can try to do something like this:如果我没听错,你可以尝试做这样的事情:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (pictureBox1.Image != null)
    {
        if ((e.Button == 0))
        {
        }
        else
        {
            pictureBox1.Padding = new Padding(e.X, e.Y, 0, 0);
        }
    }
}

Not sure what you meant, but if you want to change the position of the image inside your picture box, that's the way to look.不确定您的意思,但如果您想更改图片框内图像的位置,这就是查看方式。 In your original code, you were changing the position of the picture box itself, not the image.在您的原始代码中,您正在更改图片框本身的位置,而不是图像。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM