VintaSoft Imaging .NET SDK v8.6
In This Topic
    Undo/redo changes in images
    In This Topic

    SDK has the open architecture for undo/redo changes in images, annotations and user defined objects.
    Architecture contains 3 main classes:


    Class UndoAction allows to:

    Class UndoManager allows to:

    Here is a list of standard undo monitors:
    Here is an example that demonstrates how to undo changes in image and annotations:
    ''' <summary>
    ''' Run UndoManager example.
    ''' </summary>
    Public Sub Run()
        ' create an instance of undo manager
        Dim undoManager As New Vintasoft.Imaging.Undo.UndoManager()
    
        ' open an image with annotations
        Using image As New Vintasoft.Imaging.VintasoftImage("..\..\Resources\annotatedImage.png")
            ' create an image collection
            Dim images As New Vintasoft.Imaging.ImageCollection()
            ' add image to the image collection
            images.Add(image)
    
            ' create the annotation data controller for image collection
            Dim annotationDataController As New Vintasoft.Imaging.Annotation.AnnotationDataController(images)
    
            ' get reference to the annotation data collection of image
            Dim annotationDataCollection As Vintasoft.Imaging.Annotation.AnnotationDataCollection = annotationDataController.GetAnnotations(image)
            ' get reference to the annotation view collection of image
            Dim annotationViewCollection As New Vintasoft.Imaging.Annotation.UI.AnnotationViewCollection(annotationDataCollection)
    
            ' show information about image and annotations
            ShowInfo(image, annotationDataCollection)
    
            ' create processing command
            Dim resizeCommand As New Vintasoft.Imaging.ImageProcessing.ResizeCommand()
            ' create the undo manager for image processing
            Using imageProcessingUndoMonitor As Vintasoft.Imaging.Undo.UndoMonitor = New Vintasoft.Imaging.Undo.ImageProcessingUndoMonitor(undoManager, resizeCommand)
                ' create the undo manager for annotations
                Using annotationUndoMonitor As Vintasoft.Imaging.Undo.UndoMonitor = New Vintasoft.Imaging.Annotation.UI.Undo.AnnotationViewCollectionUndoMonitor(undoManager, annotationViewCollection)
                    ' Step 1
    
                    ' create a rectangle annotation
                    Dim rectangleAnnotationData As New Vintasoft.Imaging.Annotation.RectangleAnnotationData()
                    rectangleAnnotationData.Location = New System.Drawing.PointF(60, 110)
                    rectangleAnnotationData.Size = New System.Drawing.SizeF(100, 38)
                    ' add the rectangle annotation to the image
                    annotationDataCollection.Add(rectangleAnnotationData)
    
                    ' show information about image and annotations
                    ShowInfo(image, annotationDataCollection)
    
                    ' undo Step 1
                    undoManager.Undo(1)
    
                    ' show information about image and annotations
                    ShowInfo(image, annotationDataCollection)
    
                    ' Step 2: resize image
                    resizeCommand.Width = 550
                    resizeCommand.Height = 450
                    resizeCommand.ExecuteInPlace(image)
    
                    ' show information about image and annotations
                    ShowInfo(image, annotationDataCollection)
    
                    ' Step 3
    
                    ' change an existing annotation
                    Dim annotationData As Vintasoft.Imaging.Annotation.AnnotationData = annotationDataCollection(0)
                    annotationData.Location = New System.Drawing.PointF(15, 10)
    
                    ' show information about image and annotations
                    ShowInfo(image, annotationDataCollection)
    
                    ' undo Step 3
                    undoManager.Undo(3)
    
                    ' show information about image and annotations
                    ShowInfo(image, annotationDataCollection)
    
                    ' redo Step 2
                    undoManager.Redo(2)
    
                    ' show information about image and annotations
                    ShowInfo(image, annotationDataCollection)
                End Using
            End Using
        End Using
    End Sub
    
    ''' <summary>
    ''' Shows information about image and annotations.
    ''' </summary>
    ''' <param name="image">Image.</param>
    ''' <param name="annotationDataCollection">Annotation data collection.</param>
    Public Sub ShowInfo(image As Vintasoft.Imaging.VintasoftImage, annotationDataCollection As Vintasoft.Imaging.Annotation.AnnotationDataCollection)
        ' show information about image
        System.Console.WriteLine("Image: {0}x{1} PixelFormat:{2}", image.Width, image.Height, image.PixelFormat)
    
        ' show information about annotations
        System.Console.WriteLine("Annotation count: {0}", annotationDataCollection.Count)
        For Each annotationData As Vintasoft.Imaging.Annotation.AnnotationData In annotationDataCollection
            Dim annotationDataType As System.Type = annotationData.[GetType]()
            System.Console.WriteLine(vbTab & "Type:{0} Location:{1} Size:{2}", annotationDataType.Name, annotationData.Location, annotationData.Size)
        Next
    End Sub
    
    ' This code example produces the following output:
    '
    '        Image: 500x418 PixelFormat:Bgr24
    '        Annotations count: 2
    '                Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
    '                Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    '        Image: 500x418 PixelFormat:Bgr24
    '        Annotations count: 3
    '                Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
    '                Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    '                Type:RectangleAnnotationData Location:{X=60, Y=110} Size:{Width=100, Height=38}
    '        Image: 500x418 PixelFormat:Bgr24
    '        Annotations count: 2
    '                Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
    '                Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    '        Image: 550x450 PixelFormat:Bgr24
    '        Annotations count: 2
    '                Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
    '                Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    '        Image: 500x418 PixelFormat:Bgr24
    '        Annotations count: 2
    '                Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
    '                Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    '        Image: 550x450 PixelFormat:Bgr24
    '        Annotations count: 2
    '                Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
    '                Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    '
    
    
                  
    
    /// <summary>
    /// Run UndoManager example.
    /// </summary>
    public void Run()
    {
        // create an instance of undo manager
        Vintasoft.Imaging.Undo.UndoManager undoManager = 
            new Vintasoft.Imaging.Undo.UndoManager();
    
        // open an image with annotations
        using (Vintasoft.Imaging.VintasoftImage image = 
            new Vintasoft.Imaging.VintasoftImage(@"..\..\Resources\annotatedImage.png"))
        {
            // create an image collection
            Vintasoft.Imaging.ImageCollection images = 
                new Vintasoft.Imaging.ImageCollection();
            // add image to the image collection
            images.Add(image);
    
            // create the annotation data controller for image collection
            Vintasoft.Imaging.Annotation.AnnotationDataController annotationDataController = 
                new Vintasoft.Imaging.Annotation.AnnotationDataController(images);
    
            // get reference to the annotation data collection of image
            Vintasoft.Imaging.Annotation.AnnotationDataCollection annotationDataCollection = 
                annotationDataController.GetAnnotations(image);
            // get reference to the annotation view collection of image
            Vintasoft.Imaging.Annotation.UI.AnnotationViewCollection annotationViewCollection = 
                new Vintasoft.Imaging.Annotation.UI.AnnotationViewCollection(annotationDataCollection);
    
            // show information about image and annotations
            ShowInfo(image, annotationDataCollection);
    
            // create processing command
            Vintasoft.Imaging.ImageProcessing.ResizeCommand resizeCommand = 
                new Vintasoft.Imaging.ImageProcessing.ResizeCommand();
            // create the undo manager for image processing
            using (Vintasoft.Imaging.Undo.UndoMonitor imageProcessingUndoMonitor = 
                new Vintasoft.Imaging.Undo.ImageProcessingUndoMonitor(undoManager, resizeCommand))
            {
                // create the undo manager for annotations
                using (Vintasoft.Imaging.Undo.UndoMonitor annotationUndoMonitor = 
                    new Vintasoft.Imaging.Annotation.UI.Undo.AnnotationViewCollectionUndoMonitor(
                        undoManager, annotationViewCollection))
                {
                    // Step 1
    
                    // create a rectangle annotation
                    Vintasoft.Imaging.Annotation.RectangleAnnotationData rectangleAnnotationData = 
                        new Vintasoft.Imaging.Annotation.RectangleAnnotationData();
                    rectangleAnnotationData.Location = new System.Drawing.PointF(60, 110);
                    rectangleAnnotationData.Size = new System.Drawing.SizeF(100, 38);
                    // add the rectangle annotation to the image
                    annotationDataCollection.Add(rectangleAnnotationData);
    
                    // show information about image and annotations
                    ShowInfo(image, annotationDataCollection);
    
                    // undo Step 1
                    undoManager.Undo(1);
    
                    // show information about image and annotations
                    ShowInfo(image, annotationDataCollection);
    
                    // Step 2: resize image
                    resizeCommand.Width = 550;
                    resizeCommand.Height = 450;
                    resizeCommand.ExecuteInPlace(image);
    
                    // show information about image and annotations
                    ShowInfo(image, annotationDataCollection);
    
                    // Step 3
    
                    // change an existing annotation
                    Vintasoft.Imaging.Annotation.AnnotationData annotationData = annotationDataCollection[0];
                    annotationData.Location = new System.Drawing.PointF(15, 10);
    
                    // show information about image and annotations
                    ShowInfo(image, annotationDataCollection);
    
                    // undo Step 3
                    undoManager.Undo(3);
    
                    // show information about image and annotations
                    ShowInfo(image, annotationDataCollection);
    
                    // redo Step 2
                    undoManager.Redo(2);
    
                    // show information about image and annotations
                    ShowInfo(image, annotationDataCollection);
                }
            }
        }
    }
    
    /// <summary>
    /// Shows information about image and annotations.
    /// </summary>
    /// <param name="image">Image.</param>
    /// <param name="annotationDataCollection">Annotation data collection.</param>
    public void ShowInfo(
        Vintasoft.Imaging.VintasoftImage image,
        Vintasoft.Imaging.Annotation.AnnotationDataCollection annotationDataCollection)
    {
        // show information about image
        System.Console.WriteLine("Image: {0}x{1} PixelFormat:{2}", image.Width, image.Height, image.PixelFormat);
    
        // show information about annotations
        System.Console.WriteLine("Annotation count: {0}", annotationDataCollection.Count);
        foreach (Vintasoft.Imaging.Annotation.AnnotationData annotationData in annotationDataCollection)
        {
            System.Type annotationDataType = annotationData.GetType();
            System.Console.WriteLine("\tType:{0} Location:{1} Size:{2}", annotationDataType.Name,
                annotationData.Location, annotationData.Size);
        }
    }
    
    /* This code example produces the following output:
    
    Image: 500x418 PixelFormat:Bgr24
    Annotations count: 2
            Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
            Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    Image: 500x418 PixelFormat:Bgr24
    Annotations count: 3
            Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
            Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
            Type:RectangleAnnotationData Location:{X=60, Y=110} Size:{Width=100, Height=38}
    Image: 500x418 PixelFormat:Bgr24
    Annotations count: 2
            Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
            Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    Image: 550x450 PixelFormat:Bgr24
    Annotations count: 2
            Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
            Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    Image: 500x418 PixelFormat:Bgr24
    Annotations count: 2
            Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
            Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    Image: 550x450 PixelFormat:Bgr24
    Annotations count: 2
            Type:PolygonAnnotationData Location:{X=26,00019, Y=22,55438} Size:{Width=60,77151, Height=61,08478}
            Type:LineAnnotationData Location:{X=114,9647, Y=15,34951} Size:{Width=0, Height=104,6273}
    */