Hi,I am able to draw rectangles on the picture but I am not able to capture the co-ordinates of the picture box instead it capture the screen coordinates.So the rectangle that I draw on the picture box doesn't appear on the position that I desire.

// Convert and normalize the points and draw the reversible frame.

private void MyDrawReversibleRectangle(Point p1, Point p2)
        {
            Rectangle rc = new Rectangle();

            // Convert the points to screen coordinates.
            p1 = this.pictureBox1.PointToScreen  (p1);
            p2 = this.pictureBox1.PointToScreen (p2);
            // Normalize the rectangle.
            if (p1.X < p2.X)
            {
                rc.X = p1.X;
                rc.Width = p2.X - p1.X;
            }
            else
            {
                rc.X = p2.X;
                rc.Width = p1.X - p2.X;
            }
            if (p1.Y < p2.Y)
            {
                rc.Y = p1.Y;
                rc.Height = p2.Y - p1.Y;
            }
            else
            {
                rc.Y = p2.Y;
                rc.Height = p1.Y - p2.Y;
            }
            
            textBox1.Text = Convert.ToString(rc.X);
            textBox2.Text = Convert.ToString(rc.Y);
            textBox3.Text = Convert.ToString(rc.Width);
            textBox4.Text = Convert.ToString(rc.Height);
            // Draw the reversible frame.
            ControlPaint.DrawReversibleFrame(rc,
                            Color.Black, FrameStyle.Thick);

        }
private void button1_Click(object sender, EventArgs e)
        {
            
            motionDetector = new MotionDetector(simpleBackgroundModeling, blobCounting);
            //string[] s = File.ReadAllLines("MotionZone.txt");
            int x = Convert.ToInt32 (textBox1.Text);
            int y = Convert.ToInt32(textBox2.Text);
            int width = Convert.ToInt32(textBox3.Text);
            int height = Convert.ToInt32(textBox4.Text);
            bflag = true;
           // btnVideoStop.Enabled = true;
           // btnPlay.Enabled = false;
            rectangle[1] = new Rectangle(x, y, width, height);

Dani AI

Generated

This is a Windows Forms issue (your code calls PictureBox.PointToScreen and ControlPaint.DrawReversibleFrame), so the symptom is expected: the reversible frame is being drawn in screen coordinates but you are saving those screen coords directly for use inside the PictureBox/image. That makes the selection appear offset. , convert the screen rectangle back to the PictureBox client coordinates and then, if your motion detector expects image pixels, map those client coords into image pixels (how you map depends on PictureBox.SizeMode). was right to ask which app type — the code shows WinForms behavior.

Use this pattern:

  1. Track start/end in the PictureBox client coordinates (MouseDown/MouseMove/MouseUp).
  2. For on-screen feedback convert those points to screen coords and draw the reversible frame.
  3. On MouseUp convert the reversible frame top-left back with pictureBox1.PointToClient(...) to get clientRect.
  4. If pictureBox1.Image != null convert clientRect to image pixels — different math for StretchImage vs Zoom.

Example conversion (illustrative):

// screenRect is the rectangle used with DrawReversibleFrame
Point clientTopLeft = pictureBox1.PointToClient(new Point(screenRect.X, screenRect.Y));
Rectangle clientRect = new Rectangle(clientTopLeft.X, clientTopLeft.Y, screenRect.Width, screenRect.Height);

// StretchImage mapping
var img = pictureBox1.Image;
float sx = (float)img.Width / pictureBox1.Width;
float sy = (float)img.Height / pictureBox1.Height;
Rectangle imageRect = new Rectangle(
    (int)(clientRect.X * sx),
    (int)(clientRect.Y * sy),
    (int)(clientRect.Width * sx),
    (int)(clientRect.Height * sy));

For SizeMode.Zoom compute the displayed image size and offsets, subtract offsets from clientRect, then scale by img.Width/displayWidth (or img.Height/displayHeight). Clamp coordinates to image bounds and use the resulting imageRect for rectangle[1]. For permanent visuals draw in the PictureBox Paint event (invalidate after setting the final rectangle) rather than relying on reversible frames. Also watch for container offsets, scrollbars or DPI scaling which will change the mapping.

Is this an ASP app or a Windows Forms app?
How are you capturing the points?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.