tags:

views:

31

answers:

2

Hello, 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.Navy;
            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!

A: 

Shouldn't you be pushing the current state of pictureBox1 before performing the brush stroke?

Will
I don't know, how would I do this? undoStack.Push(pictureBox1); ?
Tanner
A: 

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.

Femaref
OK so could I use undoStack.Push(pictureBox1.Image); or how could I do this?
Tanner
I would save it as a byte[], as this isn't a reference to the image itself.
Femaref
OK, thanks for your help. :)
Tanner
mind accepting the answer then? :p
Femaref