Now my purpose is that after I draw a rectangle I want to clear it. But I don't know how to do this !
So I have try to search on the stackoverflow ,but they seems not meet my questions. and now what my code functions is draw a rectangle on the imagebox , when the mouse down , it give the start point to the rectangle also the status of mouse move! And when the mouse up , it would have the endpoint . SO using {Draw(Rectangle,TColor,Int32,LineType,Int32);} then it would generate a new rectangle; but I don't know how to remove the rectangle after that
here my code
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)//mousedown
{
if (img != null)
{
mouseStatus = true;
startPoint.X = e.X;
startPoint.Y = e.Y;
//A new rectangle resets in a new coordinate
minStartX = e.X;
minStartY = e.Y;
maxEndX = e.X;
maxEndY = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)//mousemove
{
string StringX, StringY, StringSX, StringSY;
if (mouseStatus)
{
endPoint.X = e.X;
endPoint.Y = e.Y;
//This section is to get the top and bottom left coordinates of the rectangle to be drawn. If not, the rectangle can only be drawn from the top left to the bottom right.
int realStartX = Math.Min(startPoint.X, endPoint.X);
int realStartY = Math.Min(startPoint.Y, endPoint.Y);
int realEndX = Math.Max(startPoint.X, endPoint.X);
int realEndY = Math.Max(startPoint.Y, endPoint.Y);
minStartX = Math.Min(minStartX, realStartX);
minStartY = Math.Min(minStartY, realStartY);
maxEndX = Math.Max(maxEndX, realEndX);
maxEndY = Math.Max(maxEndY, realEndY);
currRect = new Rectangle(realStartX, realStartY, realEndX - realStartX, realEndY - realStartY);
StringX = Convert.ToString(realStartX);
StringY = Convert.ToString(realStartY);
StringSX = Convert.ToString(realEndX - realStartX);
StringSY = Convert.ToString(realEndY - realStartY);
textBox1.Text = StringX;
textBox2.Text = StringY;
textBox3.Text = StringSX;
textBox4.Text = StringSY;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)/mouseup
{
mouseStatus = false;
endPoint.X = e.X;
endPoint.Y = e.Y;
img.Draw(currRect, new Bgr(Color.Red), 1);
pictureBox1.Image = img.Bitmap;
}