Saving and loading annotations
In This Topic
AnnotationData class implements ISerializable interface and can be serialized using a formatter, which implements IFormatter interface.
Standard formatters
SDK provides the following standard formatters:
- AnnotationVintasoftBinaryFormatter - allows to serialize Vintasoft annotations into "Vintasoft binary" annotation format. The format is developed by VintaSoft LLC for storing annotations in binary form.
- AnnotationVintasoftXmpFormatter - allows to serialize Vintasoft annotations into "Vintasoft XMP" annotation format. The format is developed by VintaSoft LLC for storing annotations in XMP packet.
- AnnotationWangFormatter - allows to serialize Vintasoft annotations into WANG annotation format.
- Vintasoft.Imaging.Annotation.Web.Services.AnnotationJsonFormatter - allows to serialize Vintasoft annotations into "VintaSoft JSON" annotation format.
Standard formatter or custom formatter, which implements IFormatter interface, will be able to serialize the annotation data if all annotation properties have types, which support ISerializable interface, or have one of the following standard 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 can be found in article
"Annotations supported by WANG standard specification". The WANG annotation packet can be saved either separately or as part of TIFF file.
Here is C#/VB.NET code that shows how to work with AnnotationVintasoftBinaryFormatter class:
// 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);
}
' 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
Here is C#/VB.NET code that shows how to work with AnnotationVintasoftXmpFormatter class:
// 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);
}
' 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
Here is C#/VB.NET code that shows how to work with AnnotationWangFormatter class:
// 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);
}
' 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
.NET formatters. 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. Custom formatter must be able to serialize standard types (see the list of types above) and types implementing ISerializable interface.