m0ve from point to another point

Anonymous
2021-03-26T14:32:55.743+00:00

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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 47,966 Reputation points
    2021-03-27T16:55:42.143+00:00

    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 additional answer

Sort by: Most helpful
  1. Michael Taylor 47,966 Reputation points
    2021-03-26T16:02:12.223+00:00

    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.