$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ asked $$ANON_USER$$ edited

m0ve from point to another point

Hello there. I have a picture. When I click with mouse I get the x1 and y1 coordinates. The second time I click, I get the x2 and y2 coordinates. I want to move the picture from the first point to the second point. I want to change the x1 point of the picture to x2.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered $$ANON_USER$$ edited

I think I see what you're trying to do. Your picturebox click handler is storing the left point when the user left clicks and the right point when the user right clicks. Then when the button is clicked you are moving the pixels between the bounding box defined by those 2 points but into a second picture box. An interesting approach but let's solve the problem that you have.

I believe your picturebox click logic is fine so I'm ignoring that. However I still believe you're missing some information here. You are identifying the bounding box in PB1 with the left/right mouse clicks but you aren't specifying where inside PB2 to draw the new image. Here's the logic of your algorithm.

  1. Get the width and height of the source image.

  2. Get the starting point and the offset to use. Hence if the user selects starting point (10,20) and then clicks point (40, 30) then the image is being shifted over 40 and down 30 starting with point (10, 20) until the end of the image.

Based upon your original post it sounds like what you are trying to do is move the entire image, not from the starting point on. Thus your starting point is really just a reference point on the first image and the second click is used to get the offset that you want to move the image. Is that a correct assessment of what you want?

If so then your button click just needs to get the offset given the first and second points. Then apply the offset to every pixel in the original image to move the entire image the given offset. Since you're using a picturebox here you can actually just adjust the padding of the image based upon the offset to get it to render in the second control correctly. Of course if you actually wanted to change the image then it would require you transforming the image.

private void button1_Click ( object sender, EventArgs e )
   int x2 = Convert.ToInt32(textBox1.Text);
   int y2 = Convert.ToInt32(textBox2.Text);

   int Tx = Convert.ToInt32(textBox3.Text);
   int Ty = Convert.ToInt32(textBox4.Text);

   //Get the offset from the first and second points
   int offsetX = Tx - x2;
   int offsetY = Ty - y2;

   //Create a new image of the same size as the original image
   var source = pictureBox1.Image;
   var target = new Bitmap(source);

   //Use padding to offset the original image in the control
   pictureBox2.Padding = new Padding(offsetX, offsetY, 0, 0);
   pictureBox2.Image = target;

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much for your valuable comment. Finally someone who understood me came out. I literally want something like this. Bro I talked to about 10 people and nobody understood exactly what I wanted. Thank you for taking your time and explaining it so well. You are number one :)

I managed to run the code clearly. It was a good development for my project thanks :)

0 Votes 0 ·
cooldadtx avatar image
0 Votes"
cooldadtx answered $$ANON_USER$$ commented

To move a control programmatically at runtime set the control's Location property.

pictureBox2.Location = E.Location;

Note that this is a non-standard approach to moving a control and requires that you track mouse clicks inside and outside the picture box. A better approach might be to implement it via mouse drag. When the user left click holds the mouse over the picturebox you enter drag mode. When they release the mouse then you move the control to the new location. Of course you can go for a full drag and drop implementation as well.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your answer. But i don't want to carry the picturebox. I want to move the image in the picturebox.

0 Votes 0 ·

So you want to move the image within the picture box? Exactly how does that work? A picturebox contains an Image. That image can be of anything. When the user clicks inside the picture box you can get a pixel out of that image but how do you know how much of the remainder of the image to select? Are you letting the user select a rectangle of the image and then you want to move that rectangular portion of the image?

In your example it is clear there is an image inside a white box but the Image associated with the picturebox is the entire image. Without heuristics there is no way to know that the pixel the user clicked on is inside or outside what you consider to be the "image" of the image in the control. Imagine that the picturebox contained an image that took up the entire control area. How do you intend for the user to select the portion of the image to move from that?

0 Votes 0 ·

I did this in the codes I shared. It's just moving in the wrong direction. Out of the box I had the picture cropped. With the "if" command. If you want I can send you the source file. It works. But it's just getting in the wrong direction. I could not solve this problem....

0 Votes 0 ·