0

I want an Image to move inside the picturebox. It shouldn't be possible that you can drag it out. I found an answer with the Padding and tried it out but it drags in the opposite direction. So I tried out to switch it with Right and down, but it is not getting dragged. Also I found an answer where the picturebox get moved but then it can be moved out of the form and isn't there anymore. So I need something that can just move the picture inside the picturebox or something that moves the picturebox but not out of the form.

private bool Dragging;
private Point lastLocation;


    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            Dragging = true;
            lastLocation = e.Location;
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
       if (Dragging == true)
       {
            int dx = e.X - lastLocation.X;
            int dy = e.Y - lastLocation.Y;


            pictureBox1.Padding = new Padding(0, 0, Padding.Right - dx, Padding.Bottom - dy);

            pictureBox1.Invalidate();

       }

    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        Dragging = false;
    }
Coder
  • 338
  • 2
  • 19
Neard
  • 29
  • 8

2 Answers2

0

Do this

pictureBox1.Padding = new Padding(Padding.Left + dx, Padding.Top + dy, Padding.Right - dx, Padding.Bottom - dy);

instead of this

pictureBox1.Padding = new Padding(0, 0, Padding.Right - dx, Padding.Bottom - dy);
  • if you do pictureBox1.Padding = new Padding(Padding.Left + dx, Padding.Top + dy, Padding.Right - dx, Padding.Bottom - dy); it slows down. But the thing is it still down moves right. If I want go left it does, but if i try to drag more right it doesnt – Neard Jan 22 '19 at 11:12
  • Okay, what actually should it do. It works fine. Do you mean, you can't move the picture outside the box to the top and left? – J.B. Schubert Jan 22 '19 at 11:19
0

I have done it by creating a panel and inserted image box inside of it.It's working in my side.Please find the code blow .

 private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            int moveLeftRight = e.X + pictureBox1.Left - MouseDownLocation.X;
            int moveUpDown = e.Y + pictureBox1.Top - MouseDownLocation.Y;
            int panlTopLocation = panel1.Location.Y;
            int panlbottomLocation = panel1.Location.Y + panel1.Height - pictureBox1.Height;
            int panlLeftLocation = panel1.Location.X;
            int panlRightLocation = panel1.Location.X + panel1.Width - pictureBox1.Width ;

            if (panlLeftLocation < moveLeftRight)
            {
                if (panlRightLocation > moveLeftRight)
                {
                    pictureBox1.Left = moveLeftRight;
                }
                else
                {
                    pictureBox1.Left = panlRightLocation;
                }
            }
            else
            {
                pictureBox1.Left = panlLeftLocation;
            }


            if (panlTopLocation < moveUpDown)
            {
                if (panlbottomLocation > moveUpDown)
                {
                    pictureBox1.Top = moveUpDown;
                }
                else
                {
                    pictureBox1.Top = panlbottomLocation;
                }
            }
            else
            {
                pictureBox1.Top = panlTopLocation;
            }

        }
    }
Abinash
  • 471
  • 3
  • 13
  • 1
    I already used something else but still thanks. I got away with the idea just to move the panel und moving now the picturebox with a hitbox – Neard Jan 23 '19 at 13:05