In my Form, I'm trying to make my undo button work. I use the pictureBox1
to draw my squares and pictureBox2
to display the drawn history.
Functionaility
On Picturebox mouseup event I'm adding all my drawn objects to my list array so later when I click on the undo button it should show me my history of rectangles I drew. So If I have 4 rectangles on picturebox1 and I click undo picturebox2 should show 3 rectangles. If I click on the undo button again picturebox2 should show me 2 rectangles.
The problem is when I click on the undo button It doesn't work as I hoped. It keeps on showing me all the rectangles I drew and doesn't show my the history of my drawn objects.
Code
Point startArea; // mouse-down position
Point currentArea; // current mouse position
bool drawingBox; // busy drawing
List<Rectangle> rectangleList = new List<Rectangle>(); //previous rectangles
List<Image> ImageCollection = new List<Image>();
int ImageHistory;
private void Test_Load(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.background;
}
private Rectangle getRectangle()
{
return new Rectangle(
Math.Min(startArea.X, currentArea.X),
Math.Min(startArea.Y, currentArea.Y),
Math.Abs(startArea.X - currentArea.X),
Math.Abs(startArea.Y - currentArea.Y));
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
currentArea = startArea = e.Location;
drawingBox = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
currentArea = e.Location;
if (drawingBox)
{
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if(drawingBox)
{
drawingBox = false;
var rc = getRectangle();
if(rc.Width > 0 && rc.Height > 0)
{
rectangleList.Add(rc);
pictureBox1.Invalidate();
}
}
ImageHistory++;
ImageCollection.Add(pictureBox1.Image);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (rectangleList.Count > 0)
{
using (var g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawRectangles(Pens.Black, rectangleList.ToArray());
pictureBox1.Invalidate();
}
}
if(drawingBox)
{
e.Graphics.DrawRectangle(Pens.Red, getRectangle());
pictureBox1.Invalidate();
}
}
private void button1_Click(object sender, EventArgs e)
{
ImageHistory--;
pictureBox2.Image = ImageCollection[ImageHistory];
}
}