Reputation: 299
What I want to do: I have a PictureBox with an image loaded in it. The user is supposed to click somewhere on the picture. After clicking, I save the coordinates where he clicked.
Then I want to create a new box as shown in the picture (NOTE: If the user clicks at the edges, it shouldn't overlap the image:
After that, I want to save all the coordinates [starting/ending] so when the user clicks again in the next form I can validate whether the click was within the box created before.
What I got so far:
private void pictureBox1_Click(object sender, EventArgs e)
{
var centerX = ((MouseEventArgs) e).X;
var centerY = ((MouseEventArgs) e).Y;
// create box with center in these coordinates
// save all the coordinates of the box, so I can check if further clicks are within the created box
}
My problem is Creating the box after a click knowing the center location of it. Kinda confused how it's supposed to be created.
Upvotes: 0
Views: 112
Reputation: 125197
Creating the box after a click knowing the center location of it. Kinda confused how it's supposed to be created.
You can simply create a square having it's center and the width:
Rectangle CreateSquare(Point center, int width)
{
return new Rectangle(center.X - width / 2, center.Y - width / 2,
width, width);
}
To use it, it's enough to handle MouseDown
event of your PictureBox
and use e.Location
:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
var r = CreateSquare(e.Location, 10);
}
Upvotes: 1