I tried to make Rectangle.Empty but it’s not working. it does nothing to the drawn rectangle. the drawn rectangle is stay and i want it to be deleted to start drawing a new rectangle.
_roiPicturebox = Rectangle.Empty;
_roiPicturebox is Rectangle.
private void PictureBoxFrame_MouseDown(object sender, MouseEventArgs e)
{
if (!_tracking)
{
_startPoint = e.Location;
_roiPicturebox = Rectangle.Empty; // Clear the existing rectangle
pictureBoxFrame.Invalidate(); // Trigger repaint to clear the previous rectangle
_drawing = true;
}
}
private void PictureBoxFrame_MouseMove(object sender, MouseEventArgs e)
{
if (_drawing)
{
System.Drawing.Point endPoint = e.Location;
_roiPicturebox = new Rectangle(Math.Min(_startPoint.X, endPoint.X),
Math.Min(_startPoint.Y, endPoint.Y),
Math.Abs(_startPoint.X - endPoint.X),
Math.Abs(_startPoint.Y - endPoint.Y));
pictureBoxFrame.Invalidate(); // Trigger repaint
}
}
private void PictureBoxFrame_MouseUp(object sender, MouseEventArgs e)
{
_drawing = false;
_tracking = true;
}
private void PictureBoxFrame_Paint(object sender, PaintEventArgs e)
{
if (_roiPicturebox != Rectangle.Empty)
{
e.Graphics.DrawRectangle(Pens.Red, _roiPicturebox);
}
}