Devanathan.S
Devanathan.S

Reputation: 1472

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. 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?..

enter image description here

Upvotes: 0

Views: 2317

Answers (3)

Devanathan.S
Devanathan.S

Reputation: 1472

Hi 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#

I thought this will help some one.

    Thanks
    Dev

Upvotes: 0

Clijsters
Clijsters

Reputation: 4266

A rectangle has 4 Points (edges):

  • Left
  • Top
  • Right
  • Bottom

If your mouse coordinates (MouseEventArgs properties) are between them, the mouse pointer is in the rectangle.

Are the mouse coordinates greater than right or bottom or lower than left / top, your mouse is outside the rectangle.

Taking @samgak`s Comment:

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.

Maybe the following link will help to understand:
How to check if a point is inside a rectangle

Upvotes: 1

P. Kouvarakis
P. Kouvarakis

Reputation: 1943

Provided images are the same size and assuming it is stored in variable imageSize (of type System.Drawing.Size) then:

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)

Upvotes: 0

Related Questions