Home : Annotations : Storage : Save embedded XMP annotation data in a Jpeg or Tiff
Q10274 - HOWTO: Save embedded XMP annotation data in a Jpeg or Tiff

This article is designed to be used with the Load methodology described in this KB article http://www.atalasoft.com/KB/article.aspx?id=10308 

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 abstractified 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

                          

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2017. InstantASP Ltd. All Rights Reserved