获取x和y之间的坐标点

时间:2016-05-18 14:07:52

标签: c# winforms

我在图片框中有一个jpeg图像,在页面加载时我正在根据x(150)和y(440)坐标绘制矩形。现在,当我鼠标移动到图片框上时,我需要通过坐标识别矩形并突出显示图像。例如,见下图..

让我们取第一个矩形,在鼠标移动矩形内的任何点我需要执行一些动作..如何找到矩形的这些x和y之间的坐标?..

enter image description here

3 个答案:

答案 0 :(得分:1)

矩形有4个点(边缘):

  • 右键

如果您的鼠标坐标(MouseEventArgs属性)位于它们之间,则鼠标指针位于矩形中。

如果鼠标坐标大于rightbottom或低于left / top,则鼠标位于矩形之外。

采取@ samgak的评论:

if(
(point.x >= rectangle.min_x) && (point.x <= rectangle.max_x) && 
(point.y >= rectangle.min_y) && (point.y <= rectangle.max_y)) {
    //do something
}

并将point替换为e正是您想要的。

以下链接可能有助于理解:
How to check if a point is inside a rectangle

答案 1 :(得分:0)

提供的图像大小相同,并假设它存储在变量imageSize(System.Drawing.Size类型)中,然后:

Size imageSize = new Size(...) // define the size here
...
int row = point.y / imageSize.height; 
int col = point.x / imageSize.width;
var rect = new Rectangle(col * imageSize.Width, row * imageSize.Height, imageSize.Width, imageSize.Height);

然后您可以使用rect在图像周围绘制框架(您可能希望将矩形膨胀几个像素)

答案 2 :(得分:0)

Samgak / Clijsters,

         I have completed my functionality

//页面级别声明

         private Rectangle SelectedRect;
         public List<Rectangle> listRec = new List<Rectangle>();

//在页面加载时添加矩形列表中的所有矩形。

 private void Highlightimage_Load(object sender, EventArgs e)

                    {

                        for (int i = 0; i < table.Rows.Count; i++)
                        {
                            int x = Convert.ToInt32(table.Rows[i][0]);
                            int y = Convert.ToInt32(table.Rows[i][1]);
                            int width = Convert.ToInt32(table.Rows[i][2]);
                            int height = Convert.ToInt32(table.Rows[i][3]);
                            SelectedRect.Size = new Size(width, height);
                            SelectedRect.X = x;
                            SelectedRect.Y = y;
                            listRec.Add(SelectedRect);

                        }

                        }

//绘制矩形

private void pictureBox1_Paint(object sender, PaintEventArgs e)

                    { Graphics g = e.Graphics;
                            foreach (Rectangle rec in listRec)
                            {
                                Pen p = new Pen(Color.Red);
                                g.DrawRectangle(p, rec);
                            }
            }
             private Rectangle MakeRectangle(int x0, int y0, int x1, int y1)
                    {
                        return new Rectangle(
                            Math.Min(x0, x1),
                            Math.Min(y0, y1),
                            Math.Abs(x0 - x1),
                            Math.Abs(y0 - y1));
                    }

//最后鼠标移动检查条件

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
                    {
                        foreach (Rectangle rec in listRec)
                        {
                            SelectedRect = MakeRectangle(rec.Left, rec.Top, rec.Right, rec.Bottom);

                            if (
                  (e.X >= SelectedRect.Left) && (e.X <= SelectedRect.Right) &&
                  (e.Y >= SelectedRect.Top) && (e.Y <= SelectedRect.Bottom))
                            {

                                MessageBox.Show("test");
                            }
                        }
                    }

也提到了这个链接         Drawing Multiple Rectangles c#

I thought this will help some one.

    Thanks
    Dev