There are many ways to add Undo / Redo feature using GdPicture Toolkits.
I give one which is simple and fast to implement.
The idea is to make an array of cloned image. Each entry of this array contains a state of the image on which we are working:
Create a public array containing the handle of the cloned image and a value defining the current array position
Code: Select all
Private Const UNDO_LEVEL = 10 'You can change this value to have an other undo/redo level
Dim arImagesUndo(0 To UNDO_LEVEL - 1) As Long
Dim nUndoIDX As Long = -1 '-1 means that we haven't yet saved image state
Create a SaveGraphicState procedure which saves the current image state. Call this procedure when you want to allow the user to undo the image modifications
Code: Select all
Private Sub SaveGraphicState()
Dim i As Integer
nUndoIDX = nUndoIDX + 1
If nUndoIDX > UNDO_LEVEL - 1 Then
Imaging1.CloseImage (arImagesUndo(0))
For i = 0 To UNDO_LEVEL - 2
arImagesUndo(i) = arImagesUndo(i + 1)
nUndoIDX = UNDO_LEVEL - 1
Next i
Else
For i = nUndoIDX + 1 To UNDO_LEVEL - 1
If arImagesUndo(i) <> 0 Then
Imaging1.CloseImage (arImagesUndo(i))
arImagesUndo(i) = 0
End If
Next i
End If
arImagesUndo(nUndoIDX) = Imaging1.CreateClonedImage(Imaging1.GetNativeImage)
End Sub
Undo the to the previous saved state
Code: Select all
Private Sub Undo()
nUndoIDX = nUndoIDX - 1
Imaging1.ClearImage (White)
Call Imaging1.DrawImage(arImagesUndo(nUndoIDX), 0, 0, Imaging1.GetWidth, Imaging1.GetHeight, InterpolationModeNearestNeighbor)
End Sub
Redo to the next saved state
Code: Select all
Private Sub Redo()
nUndoIDX = nUndoIDX + 1
Imaging1.ClearImage (White)
Call Imaging1.DrawImage(arImagesUndo(nUndoIDX), 0, 0, Imaging1.GetWidth, Imaging1.GetHeight, InterpolationModeNearestNeighbor)
End Sub
You can find an application of this method into the following samples:
- FreehandDraw vb6 sample of GdPicture Pro Imaging SDK
- FreehandDraw vb.net sample of GdPicture Pro Imaging SDK
Let me know any suggestion or question.
Loïc