Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i want to apply undo and redo operations on button in a photo editing application in c# programming , so that when i click undo the action on image in pictureBox is retrieved.
any one know how to apply that?
Posted

This s[^] has an undo implementation. You could consider using the logic behind this.
 
Share this answer
 
It really depends on how you've written your application. One way to do this is to apply a command pattern to the application where each action on your image is applied as a command. Undoing the operation would remove the last command, and redoing it would reapply the last command.

If that infrastructure is too complicated, you can apply a simple undo/redo mechanism by storing the image at each action point and then undo/redo based off that. I wouldn't recommend this option though because it is incredibly memory intensive as you will be storing multiple versions of the same image.
 
Share this answer
 
There are two ways to do undo/redo. Both involve an undo stack.

In the first, you simply put the state before the action on the stack. This is very easy, but can get memory intensive quickly, particularly for an image editor where the state is an image which can be megabytes in size.

In the second, what you put on the stack is the action that would need to be taken in order to return to the previous state. This is generally smaller (and is no larger, as in the worst case, for example applying a full-image filter, the action is simply to overwrite the entire state). However, for image editing it can be hard to formulate a simple inverse for most of the actions that you are likely to want to do.

A decent compromise between the two is to store the part of the image which is within the bounding rectangle of what the action affected (and the bounding box itself, of course). For most operations this will be only a small part of the image.
 
Share this answer
 
Yes. You need to program this feature from scratch. For example, store a circular ring of few past steps in editing in a queue of images. For example, if you use bitmap, use System.Collections.Generic.Queue<Bitmap>. Each time you commit the change, push newly modified image and remove the oldest one. Retrieve the old image some steps back on user's "Undo". This is the simplest approach, you could devise some more advanced.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900