0

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: enter image description here

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
user9248102
  • 299
  • 2
  • 9
  • So you want to create a bounding box around a click, save the polygon that represents the box, then determine if a subsequent click is on a point in that polygon? What are the rules for how large of a box you should make? When should boxes be made vs determining they were clicked in? Once you have your box-making rules, you can look into point in polygon solutions like ray-casting or determining the winding number. – Jonathon Chase May 31 '18 at 17:43
  • It's not clear what you're missing. If you know what the "Box" rectangle dimensiones are, you just need `Rectangle.Contains(Point)`. If you don't, then explain how the rectangle area should be calculated or derived from. – Jimi May 31 '18 at 17:45
  • Yes for all the questions until size. The size will be defined by me (depending on how large the image is, but it will be hard-coded in the code). The first step is going to be to select a location and create a box in the picture, second step which will include checking if the click is within the box won't create any additional boxes, it will be purely only for checking. – user9248102 May 31 '18 at 17:45
  • @Jimi I know the centered location of the box, I don't know how to create a box with a center in that location. – user9248102 May 31 '18 at 17:47
  • But how can that be?? You know the center. You know the width and height. So the top left will be center.X - width/2 , center.Y - height / 2. Um, there really is nothing to know other than understanding what 'center' and 'size' mean.. – TaW May 31 '18 at 18:11
  • Do you have something like a `List` that references some predefined "Box" shapes? How is the center position (or the upper-left location, it's the same) is calculated? You can use `e.Graphics.TranslateTransform()` to move thee world coordinates relative to the center point an draw your rectangle there: `CenterPoint.X - (Rect.Width / 2), CenterPoint.Y - (Rect.Height / 2));` – Jimi May 31 '18 at 18:13
  • What you described is a description of an application. What's the exact problem which you are facing with? – Reza Aghaei May 31 '18 at 19:26
  • @RezaAghaei Creating the box after a click knowing the center location of it. Kinda confused how it's supposed to be created. – user9248102 May 31 '18 at 19:48

1 Answers1

1

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);
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398