VintaSoft Imaging .NET SDK v8.6
In This Topic
    Save and load annotations
    In This Topic

    AnnotationData class implements Iserializable interface. It can be a subject for serialization using a formatter - class which implements IFormatter interface.


    Standard formatters

    There are implemented the following standard formatters:
    For correct work of annotation serialization algorithm using standard formatters it is necessary to ensure that the type of each annotation property can support Iserializable interface or appears as one of the following types:

    The formatters AnnotationVintasoftBinaryFormatter and AnnotationVintasoftXmpFormatter allow lossless loading and saving annotations.

    The formatter AnnotationVintasoftBinaryFormatter saves annotations into binary packet. This packet can be saved either separately or as part of TIFF, PDF, PNG or JPEG file.

    The formatter AnnotationVintasoftXmpFormatter saves annotations into XMP packet. This packet can be saved either separately or as part of TIFF, PDF or JPEG file.

    The formatter AnnotationWangFormatter can save not all supported annotation types with possibility of data loss. The detailed information about loading and saving of WANG annotations see in the article "Annotations supported by WANG standard specification". The WANG annotation packet can be saved either separately or as part of TIFF file.

    The following example shows how to work with AnnotationVintasoftBinaryFormatter class:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging
    ' - Vintasoft.Imaging.Annotation
    
    ' Create the image collection and add images to collection.
    Dim imageCollection As New Vintasoft.Imaging.ImageCollection()
    imageCollection.Add("D:\Images\AutoContrast.jpg")
    imageCollection.Add("D:\Images\AutoColors.jpg")
    
    ' Create annotation controller associated with image collection.
    Dim annotationDataController As New Vintasoft.Imaging.Annotation.AnnotationDataController(imageCollection)
    
    ' Create a rectangle annotation.
    Dim rectangleAnnotationData As New Vintasoft.Imaging.Annotation.RectangleAnnotationData()
    rectangleAnnotationData.Location = New System.Drawing.PointF(32, 27)
    rectangleAnnotationData.Size = New System.Drawing.SizeF(38, 35)
    ' Add the rectangle annotation to the annotation collection of first image.
    annotationDataController(0).Add(rectangleAnnotationData)
    
    ' Create a line annotation.
    Dim lineAnnotationData As New Vintasoft.Imaging.Annotation.LineAnnotationData()
    lineAnnotationData.Location = New System.Drawing.PointF(17, 66)
    lineAnnotationData.EndPoint = New System.Drawing.PointF(51, 50)
    ' Add the line annotation to the annotation collection of first image.
    annotationDataController(0).Add(lineAnnotationData)
    
    ' Create an ellipse annotation.
    Dim ellipseAnnotationData As New Vintasoft.Imaging.Annotation.EllipseAnnotationData()
    ellipseAnnotationData.Location = New System.Drawing.PointF(115, 45)
    ellipseAnnotationData.Size = New System.Drawing.SizeF(66, 64)
    ' Add the ellipse annotation to the annotation collection of first image.
    annotationDataController(0).Add(ellipseAnnotationData)
    
    ' Create a VintaSoft Binary formmater.
    Dim binaryFormatter As New Vintasoft.Imaging.Annotation.Formatters.AnnotationVintasoftBinaryFormatter()
    ' Create a file where VintaSoft Binary packet will be saved.
    Using file As New System.IO.FileStream("D:\Annotations.vsab", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)
        ' Get a reference to the annotation collection of first image.
        Dim annotationDataCollection As Vintasoft.Imaging.Annotation.AnnotationDataCollection = annotationDataController(0)
        ' Save annotation collection to the file using VintaSoft Binary formatter.
        binaryFormatter.Serialize(file, annotationDataCollection)
    End Using
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging
    // - Vintasoft.Imaging.Annotation
    
    // Create the image collection and add images to collection.
    Vintasoft.Imaging.ImageCollection imageCollection = new Vintasoft.Imaging.ImageCollection();
    imageCollection.Add(@"D:\Images\AutoContrast.jpg");
    imageCollection.Add(@"D:\Images\AutoColors.jpg");
    
    // Create annotation controller associated with image collection.
    Vintasoft.Imaging.Annotation.AnnotationDataController annotationDataController = 
        new Vintasoft.Imaging.Annotation.AnnotationDataController(imageCollection);
    
    // Create a rectangle annotation.
    Vintasoft.Imaging.Annotation.RectangleAnnotationData rectangleAnnotationData = 
        new Vintasoft.Imaging.Annotation.RectangleAnnotationData();
    rectangleAnnotationData.Location = new System.Drawing.PointF(32, 27);
    rectangleAnnotationData.Size = new System.Drawing.SizeF(38, 35);
    // Add the rectangle annotation to the annotation collection of first image.
    annotationDataController[0].Add(rectangleAnnotationData);
    
    // Create a line annotation.
    Vintasoft.Imaging.Annotation.LineAnnotationData lineAnnotationData = 
        new Vintasoft.Imaging.Annotation.LineAnnotationData();
    lineAnnotationData.Location = new System.Drawing.PointF(17, 66);
    lineAnnotationData.EndPoint = new System.Drawing.PointF(51, 50);
    // Add the line annotation to the annotation collection of first image.
    annotationDataController[0].Add(lineAnnotationData);
    
    // Create an ellipse annotation.
    Vintasoft.Imaging.Annotation.EllipseAnnotationData ellipseAnnotationData = 
        new Vintasoft.Imaging.Annotation.EllipseAnnotationData();
    ellipseAnnotationData.Location = new System.Drawing.PointF(115, 45);
    ellipseAnnotationData.Size = new System.Drawing.SizeF(66, 64);
    // Add the ellipse annotation to the annotation collection of first image.
    annotationDataController[0].Add(ellipseAnnotationData);
    
    // Create a VintaSoft Binary formmater.
    Vintasoft.Imaging.Annotation.Formatters.AnnotationVintasoftBinaryFormatter binaryFormatter = 
        new Vintasoft.Imaging.Annotation.Formatters.AnnotationVintasoftBinaryFormatter();
    // Create a file where VintaSoft Binary packet will be saved.
    using (System.IO.FileStream file = new System.IO.FileStream(@"D:\Annotations.vsab", 
        System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
    {
        // Get a reference to the annotation collection of first image.
        Vintasoft.Imaging.Annotation.AnnotationDataCollection annotationDataCollection = annotationDataController[0];
        // Save annotation collection to the file using VintaSoft Binary formatter.
        binaryFormatter.Serialize(file, annotationDataCollection);
    }
                    
    

    The following example shows how to work with AnnotationVintasoftXmpFormatter class:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging
    ' - Vintasoft.Imaging.Annotation
    
    ' Create the image collection and add images to collection.
    Dim imageCollection As New Vintasoft.Imaging.ImageCollection()
    imageCollection.Add("D:\Images\AutoContrast.jpg")
    imageCollection.Add("D:\Images\AutoColors.jpg")
    
    ' Create annotation controller associated with image collection.
    Dim annotationDataController As New Vintasoft.Imaging.Annotation.AnnotationDataController(imageCollection)
    
    ' Create a rectangle annotation.
    Dim rectangleAnnotationData As New Vintasoft.Imaging.Annotation.RectangleAnnotationData()
    rectangleAnnotationData.Location = New System.Drawing.PointF(32, 27)
    rectangleAnnotationData.Size = New System.Drawing.SizeF(38, 35)
    ' Add the rectangle annotation to the annotation collection of first image.
    annotationDataController(0).Add(rectangleAnnotationData)
    
    ' Create a line annotation.
    Dim lineAnnotationData As New Vintasoft.Imaging.Annotation.LineAnnotationData()
    lineAnnotationData.Location = New System.Drawing.PointF(17, 66)
    lineAnnotationData.EndPoint = New System.Drawing.PointF(51, 50)
    ' Add the line annotation to the annotation collection of first image.
    annotationDataController(0).Add(lineAnnotationData)
    
    ' Create an ellipse annotation.
    Dim ellipseAnnotationData As New Vintasoft.Imaging.Annotation.EllipseAnnotationData()
    ellipseAnnotationData.Location = New System.Drawing.PointF(115, 45)
    ellipseAnnotationData.Size = New System.Drawing.SizeF(66, 64)
    ' Add the ellipse annotation to the annotation collection of first image.
    annotationDataController(0).Add(ellipseAnnotationData)
    
    ' Create a VintaSoft XMP formmater.
    Dim xmpFormatter As New Vintasoft.Imaging.Annotation.Formatters.AnnotationVintasoftXmpFormatter()
    ' Create a file where VintaSoft XMP packet will be saved.
    Using file As New System.IO.FileStream("D:\Annotations.xmp", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)
        ' Get a reference to the annotation collection of first image.
        Dim annotationDataCollection As Vintasoft.Imaging.Annotation.AnnotationDataCollection = annotationDataController(0)
        ' Save annotation collection to the file using VintaSoft XMP formatter.
        xmpFormatter.Serialize(file, annotationDataCollection)
    End Using
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging
    // - Vintasoft.Imaging.Annotation
    
    // Create the image collection and add images to collection.
    Vintasoft.Imaging.ImageCollection imageCollection = new Vintasoft.Imaging.ImageCollection();
    imageCollection.Add(@"D:\Images\AutoContrast.jpg");
    imageCollection.Add(@"D:\Images\AutoColors.jpg");
    
    // Create annotation controller associated with image collection.
    Vintasoft.Imaging.Annotation.AnnotationDataController annotationDataController = 
        new Vintasoft.Imaging.Annotation.AnnotationDataController(imageCollection);
    
    // Create a rectangle annotation.
    Vintasoft.Imaging.Annotation.RectangleAnnotationData rectangleAnnotationData = 
        new Vintasoft.Imaging.Annotation.RectangleAnnotationData();
    rectangleAnnotationData.Location = new System.Drawing.PointF(32, 27);
    rectangleAnnotationData.Size = new System.Drawing.SizeF(38, 35);
    // Add the rectangle annotation to the annotation collection of first image.
    annotationDataController[0].Add(rectangleAnnotationData);
    
    // Create a line annotation.
    Vintasoft.Imaging.Annotation.LineAnnotationData lineAnnotationData = 
        new Vintasoft.Imaging.Annotation.LineAnnotationData();
    lineAnnotationData.Location = new System.Drawing.PointF(17, 66);
    lineAnnotationData.EndPoint = new System.Drawing.PointF(51, 50);
    // Add the line annotation to the annotation collection of first image.
    annotationDataController[0].Add(lineAnnotationData);
    
    // Create an ellipse annotation.
    Vintasoft.Imaging.Annotation.EllipseAnnotationData ellipseAnnotationData = 
        new Vintasoft.Imaging.Annotation.EllipseAnnotationData();
    ellipseAnnotationData.Location = new System.Drawing.PointF(115, 45);
    ellipseAnnotationData.Size = new System.Drawing.SizeF(66, 64);
    // Add the ellipse annotation to the annotation collection of first image.
    annotationDataController[0].Add(ellipseAnnotationData);
    
    // Create a VintaSoft XMP formmater.
    Vintasoft.Imaging.Annotation.Formatters.AnnotationVintasoftXmpFormatter xmpFormatter = 
        new Vintasoft.Imaging.Annotation.Formatters.AnnotationVintasoftXmpFormatter();
    // Create a file where VintaSoft XMP packet will be saved.
    using (System.IO.FileStream file = new System.IO.FileStream(@"D:\Annotations.xmp", 
        System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
    {
        // Get a reference to the annotation collection of first image.
        Vintasoft.Imaging.Annotation.AnnotationDataCollection annotationDataCollection = annotationDataController[0];
        // Save annotation collection to the file using VintaSoft XMP formatter.
        xmpFormatter.Serialize(file, annotationDataCollection);
    }
                    
    

    The following example shows how to work with AnnotationWangFormatter class:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging
    ' - Vintasoft.Imaging.Annotation
    
    ' Create the image collection and add images to collection.
    Dim imageCollection As New Vintasoft.Imaging.ImageCollection()
    imageCollection.Add("D:\Images\AutoContrast.jpg")
    imageCollection.Add("D:\Images\AutoColors.jpg")
    
    ' Create annotation controller associated with image collection.
    Dim annotationDataController As New Vintasoft.Imaging.Annotation.AnnotationDataController(imageCollection)
    
    ' Create a rectangle annotation.
    Dim rectangleAnnotationData As New Vintasoft.Imaging.Annotation.RectangleAnnotationData()
    rectangleAnnotationData.Location = New System.Drawing.PointF(32, 27)
    rectangleAnnotationData.Size = New System.Drawing.SizeF(38, 35)
    ' Add the rectangle annotation to the annotation collection of first image.
    annotationDataController(0).Add(rectangleAnnotationData)
    
    ' Create a line annotation.
    Dim lineAnnotationData As New Vintasoft.Imaging.Annotation.LineAnnotationData()
    lineAnnotationData.Location = New System.Drawing.PointF(17, 66)
    lineAnnotationData.EndPoint = New System.Drawing.PointF(51, 50)
    ' Add the line annotation to the annotation collection of first image.
    annotationDataController(0).Add(lineAnnotationData)
    
    ' Create a WANG formmater.
    Dim wangFormatter As New Vintasoft.Imaging.Annotation.Formatters.AnnotationWangFormatter(imageCollection(0).Resolution)
    ' Create a file where WANG packet will be saved.
    Using file As New System.IO.FileStream("D:\Annotations.wng", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)
        ' Get a reference to the annotation collection of first image.
        Dim annotationDataCollection As Vintasoft.Imaging.Annotation.AnnotationDataCollection = annotationDataController(0)
        ' Save annotation collection to the file using WANG formatter.
        wangFormatter.Serialize(file, annotationDataCollection)
    End Using
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging
    // - Vintasoft.Imaging.Annotation
    
    // Create the image collection and add images to collection.
    Vintasoft.Imaging.ImageCollection imageCollection = new Vintasoft.Imaging.ImageCollection();
    imageCollection.Add(@"D:\Images\AutoContrast.jpg");
    imageCollection.Add(@"D:\Images\AutoColors.jpg");
    
    // Create annotation controller associated with image collection.
    Vintasoft.Imaging.Annotation.AnnotationDataController annotationDataController = 
        new Vintasoft.Imaging.Annotation.AnnotationDataController(imageCollection);
    
    // Create a rectangle annotation.
    Vintasoft.Imaging.Annotation.RectangleAnnotationData rectangleAnnotationData = 
        new Vintasoft.Imaging.Annotation.RectangleAnnotationData();
    rectangleAnnotationData.Location = new System.Drawing.PointF(32, 27);
    rectangleAnnotationData.Size = new System.Drawing.SizeF(38, 35);
    // Add the rectangle annotation to the annotation collection of first image.
    annotationDataController[0].Add(rectangleAnnotationData);
    
    // Create a line annotation.
    Vintasoft.Imaging.Annotation.LineAnnotationData lineAnnotationData = 
        new Vintasoft.Imaging.Annotation.LineAnnotationData();
    lineAnnotationData.Location = new System.Drawing.PointF(17, 66);
    lineAnnotationData.EndPoint = new System.Drawing.PointF(51, 50);
    // Add the line annotation to the annotation collection of first image.
    annotationDataController[0].Add(lineAnnotationData);
    
    // Create a WANG formmater.
    Vintasoft.Imaging.Annotation.Formatters.AnnotationWangFormatter wangFormatter = 
        new Vintasoft.Imaging.Annotation.Formatters.AnnotationWangFormatter(imageCollection[0].Resolution);
    // Create a file where WANG packet will be saved.
    using (System.IO.FileStream file = new System.IO.FileStream(@"D:\Annotations.wng", 
        System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
    {
        // Get a reference to the annotation collection of first image.
        Vintasoft.Imaging.Annotation.AnnotationDataCollection annotationDataCollection = annotationDataController[0];
        // Save annotation collection to the file using WANG formatter.
        wangFormatter.Serialize(file, annotationDataCollection);
    }
                    
    

    Formatters of .NET. User-defined formatters.


    Besides standard formatters, there can be used any other formatter for loading and saving annotations, e.g. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.

    Also it is possible to create custom (user-defined) formatter for annotations. To this effect is necessary to implement into a formatter the serialization for simplest types (see the list of types above) and types implementing Iserializable interface.