简体   繁体   English

获取x和y之间的坐标点

[英]Get coordinates points between x and y

I have a jpeg image in a picturebox, on page load i am drawing the rectangle based on the x(150) and y(440) coordinates. 我在图片框中有一个jpeg图像,在页面加载时,我正在基于x(150)和y(440)坐标绘制矩形。 now when i mousemove on the picturebox i need to identify the rectangle by their coordinates and highlight the image. 现在,当我在图片框上移动鼠标时,我需要通过其坐标识别矩形并突出显示图像。 for example see the below image .. 例如,请参见下图。

lets take the first rectangle, on mouse move any points inside the rectangle i need to perform some actions.. how to find the coordinates between these x and y for the rectangle?.. 让我们以第一个矩形为例,在鼠标上移动矩形内的任何点时,我需要执行一些操作..如何找到矩形的这些x和y之间的坐标?

在此处输入图片说明

A rectangle has 4 Points (edges): 矩形具有4个点(边):

  • Left 剩下
  • Top 最佳
  • Right
  • Bottom 底部

If your mouse coordinates ( MouseEventArgs properties) are between them, the mouse pointer is in the rectangle. 如果鼠标坐标( MouseEventArgs属性)在它们之间,则鼠标指针位于矩形中。

Are the mouse coordinates greater than right or bottom or lower than left / top , your mouse is outside the rectangle. 鼠标坐标是否大于rightbottom或小于left / top ,则鼠标位于矩形外部。

Taking @samgak`s Comment: 以@samgak的评论:

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

and replacing point with e is exactly what you want. 而用e代替point正是您想要的。

Maybe the following link will help to understand: 也许以下链接将有助于您了解:
How to check if a point is inside a rectangle 如何检查点是否在矩形内

Provided images are the same size and assuming it is stored in variable imageSize (of type System.Drawing.Size) then: 提供的图像大小相同,并假定将其存储在变量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);

You can then use rect to draw you frame around the image (you may want to inflate the rectangle by a couple of pixels) 然后,您可以使用rect在图像周围绘制边框(您可能希望将矩形扩大几个像素)

Hi Samgak /Clijsters, 嗨Samgak / Clijsters,

         I have completed my functionality

// page level declaration //页面级声明

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

// on page load add all the rectangle in the rectanglelist. //在页面加载时,将所有矩形添加到矩形列表中。

 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);

                        }

                        }

// draw the rectangle //绘制矩形

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));
                    }

//finally on mouse move checking the condition //最后通过鼠标移动检查条件

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");
                            }
                        }
                    }

Also refered this link Drawing Multiple Rectangles c# 另请参阅此链接绘制多个矩形c#

I thought this will help some one.

    Thanks
    Dev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM