HOWTO: Save and retrieve an image with XMP Annotations


With DotImage Document Imaging, XMP annotations can be stored and loaded from a TIFF or JPEG image. Saving annotations to XMP and embedding into an image requires setting the XMP property of the TIFF or JPEG codec. The following is a VB example of saving an image with Annotations using the SaveFileDialog to specify the filename.

C#

byte[] xmpBytes = this.AnnotateViewer1.Annotations.Save();
Codec.TiffEncoder tiff = new Codec.TiffEncoder(Codec.TiffCompression.Group4FaxEncoding);
tiff.Xmp = xmpBytes;
SaveFileDialog fs = new SaveFileDialog();
if(fs.ShowDialog() == DialogResult.OK)
{
     this.AnnotateViewer1.Save(fs.FileName, tiff);
}

[VB.NET]

Dim xmpBytes As Byte() = Me.AnnotateViewer1.Annotations.Save()
Dim tiff As Codec.TiffEncoder = New Codec.TiffEncoder(Codec.TiffCompression.Group4FaxEncoding)
tiff.Xmp = xmpBytes
Dim fs As SaveFileDialog = New SaveFileDialog
If fs.ShowDialog(Me) = DialogResult.OK Then
     Me.AnnotateViewer1.Save(fs.FileName, tiff)
End If

To load the image with annotations back into the Viewer, use the XMP Parser as follows:

C#

OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
	this.AnnotateViewer1.Open(fd.FileName);
	XmpParser parse = new XmpParser();
	byte[] xmpData = parse.BytesFromImage(fd.FileName);
	this.AnnotateViewer1.Annotations.Load(xmpData, 
	                                      Atalasoft.Imaging.Annotate.AnnotationDataFormat.Xmp);
}

[VB.NET]

Dim fd As OpenFileDialog = New OpenFileDialog
If fd.ShowDialog(Me) = DialogResult.OK Then
     Me.AnnotateViewer1.Open(fd.FileName)
     Dim parse As XmpParser = New XmpParser
     Dim xmpData As Byte() = parse.BytesFromImage(fd.FileName)
     Me.AnnotateViewer1.Annotations.Load(xmpData, _
                                         Atalasoft.Imaging.Annotate.AnnotationDataFormat.Xmp)
End If

Original Article:

Q10078 - HOWTO: Save and retrieve an image with XMP Annotations