Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

winform move an image inside a picturebox

Tags:

c#

winforms

I've been trying to do this for a few hours now, but for the life of me I can't make it possible.

What I'm trying to do is simply move the image found within a picture box in a winform application. My image is roughly 1000x1000 pixels and my box is something arbitrary like 400x500, so, for example, when I click the mouse I'd want the image to move 50 to the left. But the image box should remain the same size.

For the life of me, however, I can't get this to work. What I have been able to do is the following:

     if (kinectController.hands[0].fingertips.Count == 1)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

        }

This function is for my kinect finger tracking app. So when the application finds a single finder point visiable on the screen, the image is centered. However, I would eventually like the image to move along with my finger movement, which will come once I work out the basic step of moving the image a few pixels to the side.

Any help with this would be appreciated.

like image 888
N0xus Avatar asked Sep 26 '12 14:09

N0xus


People also ask

How do you move images in PictureBox?

To move the image, click the move button, click on the image, keep hold the clicked mouse button and drag.

Which PictureBox property is used to add a picture in the picture box?

The PictureBox control is used for displaying images on the form. The Image property of the control allows you to set an image both at design time or at run time. Let's create a picture box by dragging a PictureBox control from the Toolbox and dropping it on the form.

How do I use picture box in Windows form?

Select New Project--->Visual C#-->Windows Forms App (. NET Framework), give your project a name and click OK. This action creates a WinForms project with a default form and you should see the Windows Designer. Let's add a PictureBox control to the form by dragging it from Toolbox and dropping it to the form.


2 Answers

I did a little bit of research and apparently moving an image within a PictureBox is no easy task, at the very least I couldn't find anything that would make this possible (not saying there isn't a way to do it though).

However, I came up with a bit of a "workaround", see if this fits your needs. To accomplish this:

  • Create a Panel control, and size it to however much of the image you would like to display
  • Inside that panel place a PictureBox control with your image in it and set the SizeMode property to AutoSize.

Now, put this code in your form

private bool Dragging;
private int xPos;
private int yPos;
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { Dragging = false; }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) { 
        Dragging = true;
        xPos = e.X;
        yPos = e.Y;
    }
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
    Control c = sender as Control;
    if (Dragging && c!= null) {
        c.Top = e.Y + c.Top - yPos;
        c.Left = e.X + c.Left - xPos;
    }
}

Now whenever you click and drag on the PictureBox, it won't actually move the image within it, but the PictureBox control within the panel. Again, not exactly what you were looking for and I'm not sure how this would convert over to Kinect, but I hope this gets you on the right track.

like image 77
Ben Black Avatar answered Sep 19 '22 08:09

Ben Black


Not enough reputation to comment but I wanted to add on Ben Black answer if someone ever need more control over the image moving around so you can't move the image past it's borders :

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        Control c = sender as Control;
        if (Dragging && c != null)
        {
            int maxX = pictureBox1.Size.Width * -1 + panel.Size.Width;
            int maxY = pictureBox1.Size.Height * -1 + panel.Size.Height;

            int newposLeft = e.X + c.Left - xPos;
            int newposTop = e.Y + c.Top - yPos;

            if (newposTop > 0)
            {
                newposTop = 0;
            }
            if (newposLeft > 0)
            {
                newposLeft = 0;
            }
            if (newposLeft < maxX)
            {
                newposLeft = maxX;
            }
            if (newposTop < maxY)
            {
                newposTop = maxY;
            }
            c.Top = newposTop;
            c.Left = newposLeft;

        }
    }
like image 38
Guillaume Avatar answered Sep 19 '22 08:09

Guillaume