C# – How to draw a rectangle using a mouse in C# (emgucv)

cemgucvmouse

I want to draw a rectangle using a mouse on video frame (i.e picture box), just like when we select any files. The user will click mouse button select the area and will release the mouse button. Just like snipping or crop!

I m using emgucv!

Best Answer

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.Structure;

namespace Emgucv33Apps
{
public partial class FormCropImage : Form
{
    Image<Bgr, byte> imgInput;
    Rectangle rect;
    Point StartLocation;
    Point EndLcation;
    bool IsMouseDown = false;

    public FormCropImage()
    {
        InitializeComponent();
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog()==DialogResult.OK)
        {
            imgInput = new Image<Bgr, byte>(ofd.FileName);
            pictureBox1.Image = imgInput.Bitmap;
        }
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        IsMouseDown = true;
        StartLocation = e.Location;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (IsMouseDown==true)
        {
            EndLcation = e.Location;
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        if (rect!=null)
        {
            e.Graphics.DrawRectangle(Pens.Red, GetRectangle());
        }
    }

    private Rectangle GetRectangle()
    {
        rect = new Rectangle();
        rect.X = Math.Min( StartLocation.X,EndLcation.X);
        rect.Y = Math.Min(StartLocation.Y, EndLcation.Y);
        rect.Width = Math.Abs(StartLocation.X - EndLcation.X);
        rect.Height = Math.Abs(StartLocation.Y - EndLcation.Y);

        return rect;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (IsMouseDown==true)
        {
            EndLcation = e.Location;
            IsMouseDown = false;
            if (rect!=null)
            {
                imgInput.ROI = rect;
                Image<Bgr, byte> temp = imgInput.CopyBlank();
                imgInput.CopyTo(temp);
                imgInput.ROI = Rectangle.Empty;
                pictureBox2.Image = temp.Bitmap;
            }
        }
    }
}
}