Search

Atalasoft Knowledge Base

HOWTO: Save and Load Wang Annotation Data for Multipage TIFF Files

Administrator
DotImage

Outdated Content

This article has been flagged by support for review/rewrite. The concept is generally sound but the finer points need review/rewriting. Please consider creating a support case to ask support for best practices for Multipage TIFF file annotation embedding

Original Content

There are many different ways to save annotation data for a multi page file. One of the best ways to do this is by saving the annotation data as metadata into a tiff tag. With the help of the class TiffFile, this is acctualy really simply to do.

When saving the annotation data, once you have the data into byte array form('data' in this case), this is all you need to do:

C#

FileStream tiffStream = new FileStream(@"..\..\image.tif", FileMode.Open, FileAccess.ReadWrite);

TiffFile tf = new TiffFile();
tf.Read(tiffStream);

TiffTagCollection tagCol = new TiffTagCollection();

for (int i = 0; i < tf.Images.Count; i++)
    tagCol = tf.Images[i].Tags;
    TiffTag tag = tagCol.LookupTag(TiffTagID.WangAnnotationData);
    if (tag != null) 
    {
        tagCol.Remove(tag);
    }
    tag = new TiffTag(TiffTagID.WangAnnotationData, data, TiffTagDataType.Byte);
    tagCol.Add(tag);
}

// save the tifffile
string tempPath = System.IO.Path.GetTempFileName();
tf.Save(tempPath);
tiffStream.Close();
File.Copy(tempPath, @"..\..\image.tif", true);

When loading the data into the annotate viewer, this requires a few more steps. Remember to set the MultipageAnnotateMode property to true, so that the viewer knows to switch the annotation layers with the images. The coralation between the layer index number and the image index number is what the viewer follows.

C#

using (FileStream fs = new FileStream(@"..\..\image.tif", FileMode.Open, FileAccess.Read))
{
    tiffFile tf = new TiffFile();
    tf.Read(fs);
    
    for(int i=0; i< tf.Images.Count; i++)
    {
        // load the image first
        this.annotateViewer1.Images.Insert(i, new AtalaImage(@"..\..\image.tif", i, null));
        
        // load the data
        byte[] wngData = (byte[])tf.Images[i].Tags.LookupTag(TiffTagID.WangAnnotationData).Data;
        

        Layer l = new Layer("Layer for page "+i.ToString(),false);
        this.annotateViewer1.Annotations.Layers.Insert(i, l);
        this.annotateViewer1.Annotations.CurrentLayer = l;
        if (wngData.Length > 0)
        {
            this.annotateViewer1.Annotations.Load(wngData, AnnotationDataFormat.Wang);
        }
    }
}

Original Article:
Q10130 - HOWTO: Save and Load annotation data for multipage tiff files

Details
Last Modified: Last Month
Last Modified By: Administrator
Type: HOWTO
Article not rated yet.
Article has been viewed 53 times.
Options
Also In This Category