如何在调整窗体大小时绘制矩形

时间:2017-01-25 05:47:07

标签: opencv video-processing drawrectangle

enter code here我正在使用OpenCv并显示我从图像框上的视频文件中获取的帧。现在我在框架上绘制多个矩形并尝试移动和删除它们。这工作正常。但是,当我调整表格的大小...我无法在正确的位置绘制,移动或删除矩形,因为框架坐标没有改变,只有图像框坐标正在改变,框架是装进去。请帮帮我。

黑色矩形是我在调整窗体大小后绘制矩形的地方...红色矩形是绘制矩形的位置

private void imageBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            drawRect = true;
            RectStartpt = e.Location;
            if (count > 0)
            {
                foreach (Rectangle allRect in Rect_List)
                {
                    rect_count++;
                    if (allRect.Contains(e.Location))
                    {
                        Cursor cursor = Cursors.Cross;
                        drawRect = false;
                        RectStartpt = new Point(e.X, e.Y);
                        rect = allRect;
                        break;
                    }
                }
            }
        }this.Invalidate();}
     private void imageBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left && e.Button != MouseButtons.Right)
        {
            return;
        }
        else if (e.Button == MouseButtons.Left)
        {
            Point EndPt = new Point(e.X, e.Y);
            Size shift = new Size(Math.Abs(RectStartpt.X - EndPt.X), Math.Abs(RectStartpt.Y - EndPt.Y));
            if (drawRect == true)
            {
                rect.Location = new Point(Math.Min(RectStartpt.X, EndPt.X), Math.Min(RectStartpt.Y, EndPt.Y));
                rect.Size = shift;
                count = 1;
            }
            else if (drawRect == false)
            {
                rect.X += e.X - RectStartpt.X;
                rect.Y += e.Y - RectStartpt.Y;
                RectStartpt = new Point(e.X, e.Y);

            }
        }
    }
    private void imageBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (frame != null)
        {
            if (drawRect == true)
            {
                frame.Draw(rect, new Bgr(Color.Red), 2);
                Rect_List.Add(rect);
            }
            else if (drawRect == false)
            {
                frame.Draw(rect, new Bgr(Color.Red), 2);
                Rect_List.RemoveAt(rect_count - 1);
                Rect_List.Add(rect);
                if (Timer_enabled == false)
                {
                    frame = captureFrame.QueryFrame().ToImage<Bgr, Byte>();
                    foreach (Rectangle allRect in Rect_List)
                    {
                        frame.Draw(allRect, new Bgr(Color.Red), 2);
                    }
                    imageBox1.Image = frame;
                    imageBox1.Refresh();
                    rect = new Rectangle();
                    frame_no++;
                }
            }
            imageBox1.Image = frame;
            imageBox1.Refresh();
            //rect = new Rectangle();
            rect_count = 0;
        }
    }
    private void Form1_Resize(object sender, EventArgs e)
    {
        formGraphics = null;
        formGraphics = imageBox1.CreateGraphics();
        frame = frame.Resize(imageBox1.Width, imageBox1.Height, Inter.Linear);
        imageBox1.Image = frame;
    }

0 个答案:

没有答案