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;
using System.Windows.Forms;
namespace Auto_Detection_System
{
public partial class FormDrawROI : Form
{
public FormDrawROI()
{
InitializeComponent();
}
private System.Drawing.Point rectStartPoint, rectEndPoint;
public bool b_CreateModel = false;
bool b_DrawRoi;
Rect roiRect;
private static void ConvertCoordinates(PictureBox pic, out int X0, out int Y0, int x, int y)
{
int pic_hgt = pic.ClientSize.Height;
int pic_wid = pic.ClientSize.Width;
int img_hgt = pic.Image.Height;
int img_wid = pic.Image.Width;
X0 = x;
Y0 = y;
switch (pic.SizeMode)
{
case PictureBoxSizeMode.AutoSize:
case PictureBoxSizeMode.StretchImage:
X0 = (int)(img_wid * x / (float)pic_wid);
Y0 = (int)(img_hgt * y / (float)pic_hgt);
break;
}
}
private void picBox_MouseDown(object sender, MouseEventArgs e)
{
if (true)
{
if (e.Button == MouseButtons.Left)
{
rectStartPoint = e.Location;
Invalidate();
b_DrawRoi = true;
}
else if (e.Button == MouseButtons.Right)
{
b_DrawRoi = false;
rectStartPoint = rectEndPoint;
pictureBox2.Refresh();
}
}
}
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
if (b_DrawRoi)
{
if (e.Button != MouseButtons.Left)
{
return;
}
rectEndPoint = e.Location;
pictureBox2.Invalidate();
int X0, Y0;
ConvertCoordinates(pictureBox2, out X0, out Y0, e.X, e.Y);
ConvertCoordinates(pictureBox2, out X0, out Y0, rectStartPoint.X, rectStartPoint.Y);
int X1, Y1;
ConvertCoordinates(pictureBox2, out X1, out Y1, rectEndPoint.X, rectEndPoint.Y);
roiRect = new Rect(Math.Min(X0, X1), Math.Min(Y0, Y1), Math.Abs(X0 - X1), Math.Abs(Y0 - Y1));
}
}
private void picBox_Paint(object sender, PaintEventArgs e)
{
if (rectStartPoint.Equals(rectEndPoint)) return;
e.Graphics.DrawRectangle(System.Drawing.Pens.Red, rectStartPoint.X, rectStartPoint.Y, rectEndPoint.X - rectStartPoint.X, rectEndPoint.Y - rectStartPoint.Y);
}
}
}