Сохранение и загрузка аннотаций
В этом разделе
Класс
AnnotationData реализует интерфейс ISerializable и может быть сериализован с помощью форматтера, который реализует интерфейс IFormatter.
Стандартные форматтеры
SDK предоставляет следующие стандартные форматтеры:
- AnnotationVintasoftBinaryFormatter - позволяет сериализовать Vintasoft-аннотации в формат аннотаций "Vintasoft binary". Формат разработан ВинтаСофт для хранения аннотаций в двоичном виде.
- AnnotationVintasoftXmpFormatter - позволяет сериализовать Vintasoft-аннотации в формат аннотаций "Vintasoft XMP". Формат разработан компанией ВинтаСофт для хранения аннотаций в пакете XMP.
- AnnotationWangFormatter - позволяет сериализовать Vintasoft-аннотации в формат аннотаций WANG.
- Vintasoft.Imaging.Annotation.Web.Services.AnnotationJsonFormatter - позволяет сериализовать Vintasoft-аннотации в формат аннотаций "VintaSoft JSON".
Стандартный или пользовательский форматтер, реализующий интерфейс IFormatter, сможет сериализовать данные аннотации, если все свойства аннотации имеют типы, поддерживающие интерфейс ISerializable, или имеют один из следующих стандартных типов:
Форматеры AnnotationVintasoftBinaryFormatter и AnnotationVintasoftXmpFormatter позволяют загружать и сохранять аннотации без потерь.
Форматтер AnnotationVintasoftBinaryFormatter сохраняет аннотации в двоичный пакет. Этот пакет можно сохранить отдельно или как часть файла TIFF, PDF, PNG или JPEG.
Форматтер AnnotationVintasoftXmpFormatter сохраняет аннотации в пакете XMP. Этот пакет можно сохранить отдельно или как часть файла TIFF, PDF или JPEG.
Форматтер AnnotationWangFormatter может сохранять не все поддерживаемые типы аннотаций с возможностью потери данных. Подробную информацию о загрузке и сохранении аннотаций WANG можно найти в статье
"Аннотации, поддерживаемые спецификацией WANG"
. Пакет аннотаций WANG можно сохранить отдельно или как часть файла TIFF.
Вот C#/VB.NET код, который демонстрирует, как работать с классом AnnotationVintasoftBinaryFormatter:
// 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
Вот C#/VB.NET код, который демонстрирует, как работать с классом AnnotationVintasoftXmpFormatter:
// 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
Вот C#/VB.NET код, который демонстрирует, как работать с классом AnnotationWangFormatter:
// 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-форматеры. Пользовательские форматтеры.
Помимо стандартных форматтеров, для загрузки и сохранения аннотаций можно использовать любой другой форматтер, например System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.
Также можно создать собственный (определяемый пользователем) форматтер для аннотаций. Пользовательский форматтер должен иметь возможность сериализовать стандартные типы (см. список типов выше) и типы, реализующие интерфейс ISerializable.