Search

Atalasoft Knowledge Base

HOWTO: Save embedded XMP annotation data in a Jpeg or Tiff

Administrator
DotImage

This article is designed to be used with the Load methodology described in Q10308 - HOWTO: Load Annotations from Jpeg or Tiff

Jpeg and Tiff formats allow for custom tags to be added during encoding. This allows a user of DotImage to embed XMP annotation data into an image file using the JpegEncoder or TiffEncoder. The example below is an abstracted version of saving that removes the role of the AnnotationController. This allows the same code to be used to save and load in different viewers.

C#

public void Save(System.IO.Stream s, Atalasoft.Imaging.AtalaImage image,             Atalasoft.Annotate.UI.LayerCollection layers)
{
    SetXmp(layers);
    myImageEncoder.Save(s, image, null);
}
 
private void SetXmp(Atalasoft.Annotate.UI.LayerCollection layers)
{
    XmpFormatter xmp = new XmpFormatter();
    MemoryStream mem = new MemoryStream();
    xmp.Serialize(mem, layers);
    mem.Seek(0, SeekOrigin.Begin);
    SetEncoderXmp(mem.ToArray());
}
 
private void SetEncoderXmp(byte[] p)
{
    TiffEncoder tiff = myImageEncoder as TiffEncoder;
    if (tiff != null)
        tiff.Xmp = p;
    JpegEncoder jpeg = myImageEncoder as JpegEncoder;
    if (jpeg != null)
        jpeg.Xmp = p;
}

VB.NET

Public Sub Save(ByVal s As System.IO.Stream, ByVal image As Atalasoft.Imaging.AtalaImage, ByVal layers As Atalasoft.Annotate.UI.LayerCollection)
    SetXmp(layers)
    myImageEncoder.Save(s, image, Nothing)
End Sub
 
Private Sub SetXmp(ByVal layers As Atalasoft.Annotate.UI.LayerCollection)
    Dim xmp As New XmpFormatter()
    Dim mem As New MemoryStream()
    xmp.Serialize(mem, layers)
    mem.Seek(0, SeekOrigin.Begin)
    SetEncoderXmp(mem.ToArray())
End Sub
 
Private Sub SetEncoderXmp(ByVal p As Byte())
    Dim tiff As TiffEncoder = TryCast(myImageEncoder, TiffEncoder)
    If tiff IsNot Nothing Then
        tiff.Xmp = p
    End If
    Dim jpeg As JpegEncoder = TryCast(myImageEncoder, JpegEncoder)
    If jpeg IsNot Nothing Then
        jpeg.Xmp = p
    End If
End Sub

The following snippet shows how to load back this same data into a new LayerCollection:

C#

XmpAnnotationDataImporter imp = new XmpAnnotationDataImporter(stream);
AnnotationDataCollection data = imp.Import();
LayerCollection layers = new LayerCollection();
foreach (LayerData layerdata in data)
{
    LayerAnnotation layer = new LayerAnnotation(layerdata);
    layer.CreateAnnotationUIObjects(new AnnotationUIFactoryCollection());
    layers.Add(layer); 
}

VB.NET

Dim imp As New XmpAnnotationDataImporter(stream)
Dim data As AnnotationDataCollection = imp.Import()
Dim layers As New LayerCollection()
For Each layerdata As LayerData In data
    Dim layer As New LayerAnnotation(layerdata)
    layer.CreateAnnotationUIObjects(New AnnotationUIFactoryCollection())
    layers.Add(layer)
Next

Original Article:
Q10274 - HOWTO: Save embedded XMP annotation data in a Jpeg or Tiff
Details
Last Modified: 6 Years Ago
Last Modified By: Administrator
Type: HOWTO
Article not rated yet.
Article has been viewed 807 times.
Options
Also In This Category