Im trying to create a undo function for a image editor but I having troubles. What Im trying to do is push each brush stroke through a stack then use stackName.Pop() to undo but I can't seem to add the brush stroke to the stack. Here is my code:
Stack undoStack = new Stack();
bool mouseDown = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown == true)
{
int width = 50;
int height = 50 + 1;
int top = e.Location.X - width / 2;
int left = e.Location.Y - height / 2;
Color theColor = Color.N开发者_开发技巧avy;
SolidBrush brush = new SolidBrush(theColor);
System.Drawing.Graphics graphObj = Graphics.FromHwnd(pictureBox1.Handle);
graphObj.FillEllipse(brush, new Rectangle(top, left, width, height));
undoStack.Push(graphObj);//What Do I push here?
}
}
But I don't know what to push into the stack. Please Help thank you!
Shouldn't you be pushing the current state of pictureBox1 before performing the brush stroke?
You would need to save the current state of the image, not the brush changing it, as you can't undo it directly. You could of course change the state change for each pixel changed (by comparing before and after you applied the brush), saving memory and processing time. Be aware to actually save a copy of the image, not just the reference.
精彩评论