如果您知道居中坐标,则在图片上创建相等的框

Create equal box on a picture if you know centered coordinates

我想做的事情: 我有一个 PictureBox,其中加载了图像。用户应该点击图片上的某处。点击后,我保存他点击的坐标。

然后我想创建一个新的盒子,如图所示(注意:如果用户点击边缘,它不应该与图像重叠:

之后,我想保存所有坐标 [starting/ending],这样当用户在下一个表单中再次点击时,我可以验证点击是否在之前创建的框中。

到目前为止我得到了什么:

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
}

我的问题是 在知道它的中心位置后单击后创建框。有点困惑它应该如何创建。

Creating the box after a click knowing the center location of it. Kinda confused how it's supposed to be created.

您可以简单地创建一个具有中心和宽度的正方形:

Rectangle CreateSquare(Point center, int width)
{
    return new Rectangle(center.X - width / 2, center.Y - width / 2,
        width, width);
}

要使用它,只需处理 PictureBoxMouseDown 事件并使用 e.Location:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    var r = CreateSquare(e.Location, 10);
}