Bitmap > Working with Bitmap for Silverlight > Undo and Redo History |
We all make mistakes. Having the ability to undo (and then possibly redo) mistakes in any application is extremely convenient because saves us all time from having to start over. In this sample we take a straightforward approach to enabling Undo/Redo by saving up to 3 copies of the image after each change is applied. The changes include cropping, resizing and warping. The trick is knowing how to traverse back and forth through the history, while also enabling the user to tack on more additional changes. The code for this really has nothing to do with C1Bitmap or Silverlight 4 enhancements, so it can be used to undo/redo any particular control.
C# |
Copy Code
|
---|---|
List<C1Bitmap> undoBitmaps = new List<C1Bitmap>(); List<C1Bitmap> redoBitmaps = new List<C1Bitmap>(); private void btnUndo_Click(object sender, RoutedEventArgs e) { if (undoBitmaps.Count > 1) { bitmap = new C1Bitmap(undoBitmaps.ElementAt(undoBitmaps.Count - 2)); redoBitmaps.Add(new C1Bitmap(undoBitmaps.ElementAt(undoBitmaps.Count - 1))); undoBitmaps.RemoveAt(undoBitmaps.Count - 1); UpdateImage(false); } UpdateEditButtons(); } private void btnRedo_Click(object sender, RoutedEventArgs e) { if (redoBitmaps.Count > 0) { bitmap = new C1Bitmap(redoBitmaps.ElementAt(redoBitmaps.Count - 1)); undoBitmaps.Add(new C1Bitmap(redoBitmaps.ElementAt(redoBitmaps.Count - 1))); redoBitmaps.RemoveAt(redoBitmaps.Count - 1); UpdateImage(false); } UpdateEditButtons(); } void UpdateHistory() { //Add current bitmap to memory undoBitmaps.Add(new C1Bitmap(bitmap)); redoBitmaps.Clear(); //Restrict application to only hold up to 3 instances or changes made to C1Bitmap for undo/redo history if (undoBitmaps.Count > 4) undoBitmaps.RemoveAt(0); UpdateEditButtons(); } |