Winform moves the image inside the boot window

I tried to do this for several hours, but for life I cannot make it possible.

What I'm trying to do is simply move the image found in the image window in the winform application. My image is about 1000x1000 pixels, and my field is something like 400x500, so for example, when I click the mouse button, I need the image to move 50 to the left. But the image window should remain the same size.

However, for the life of me I cannot make it work. I could do the following:
if (kinectController.hands[0].fingertips.Count == 1) { pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; } 

This feature is for my kinect finger tracking application. Therefore, when the application finds on the screen one search point visible on the screen, the image is centered. Nevertheless, in the end, I would like the image to move along with the movement of the finger, which will appear when I work out the basic step of moving the image a few pixels to the side.

Any help with this would be appreciated.

+7
source share
2 answers

I learned a little and, apparently, moved the image to the PictureBox, not an easy task, at least I could not find anything that could make this possible (without saying that there is no way to do it though).

However, I came up with a bit of a β€œworkaround” to see if this fits your needs. For this:

  • Create a Panel control and place it on any part of the image that you would like to display
  • Inside this panel, set the PictureBox control with the image in it and set the SizeMode property to AutoSize.

Now enter 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 = eX; yPos = eY; } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { Control c = sender as Control; if (Dragging && c!= null) { c.Top = eY + c.Top - yPos; c.Left = eX + c.Left - xPos; } } 

Now, when you click and drag the PictureBox, in fact it does not move the image inside it, but the PictureBox control inside the panel. Again, not exactly what you were looking for, and I'm not sure how it will convert to Kinect, but I hope this helps you on the right track.

+15
source

There is not enough reputation to comment, but I wanted to add Ben Black's answer if someone needs more control over a moving image so that you cannot move the image beyond its 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 = eX + c.Left - xPos; int newposTop = eY + 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; } } 
+1
source

Source: https://habr.com/ru/post/926316/


All Articles