Adding and Merging Annotations

Questions, comments and suggestions concerning VintaSoft Annotation .NET Plug-in.

Moderator: Alex

Post Reply
average_male
Posts: 3
Joined: Tue May 29, 2012 9:14 pm

Adding and Merging Annotations

Post by average_male »

Hello Alex,

We are demoing this product and wanted to process bulk image files in a process that doesn't require user intervention. This process simply needs to add annotations to each image (and every page in the image) and then merge the annotations with the image.

Question: Is it required to use the "AnnotationViewer" class to perform this? Or is there another "Faster" way to do this that doesn't require a viewer as no user inferace will be needed. Here is the current (pseudo) logic that requires the "AnnotationView":

Code: Select all

foreach(image in collection)
   foreach(page in image)
   {
      TextAnnotation header = new TextAnnotation();
       header.Text...
       header.Location...
       header.Size...

       annotationViewer.SelectedAnnotationCollection.Add(header);
       annotationViewer.Annotations.MergeImageWithAnnotations([i]current_page_index[/i]);
   }
If there is another way, I'd be very interested in know how this can be done.

Performance is a concern for us and we'd like to test bench this scenario with our stock images.

Thanks,
Moe
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Adding and Merging Annotations

Post by Alex »

Hello Moe,

Sorry for a delay.

You do not need to use AnnotationViewer for your code, AnnotationViewer should be used only if you want to preview images with annotations.

Here is a code snippet that show how to stamp images from image collection:

Code: Select all

// create image collection AND load multipage TIFF file into collection
ImageCollection images = new ImageCollection();
images.Add("multipage.tif");

// create annotation controller which will manage annotations of image collection AND
// link image collection and annotation controller
AnnotationController annotations = new AnnotationController(images);

// for each image in image collection
for (int i = 0; i < images.Count; i++)
{
    // create the Stamp annotation
    StampAnnotation stamp = new StampAnnotation();
    stamp.Text = "Approved";
    stamp.StampColor = Color.Blue;
    stamp.Rotation = -30;
    stamp.Location = new PointF(images[i].Width / 2, images[i].Height / 2);
    stamp.Size = new SizeF(images[i].Width / 3 * 2, images[i].Height / 5);
    annotations[i].Add(stamp);

    // merge image with annotation
    annotations.MergeImageWithAnnotations(i);
}
Best regards, Alexander
mrajkumar
Posts: 3
Joined: Tue Apr 23, 2013 3:26 pm

Re: Adding and Merging Annotations

Post by mrajkumar »

Hi Alex,
I have a smilar requirements where I want to traverse a folder and merge the (existing)annotations with the images in the background (no user interventions).
I tried your sample code but it doesnt seem to save the images.

annotations.MergeImageWithAnnotations(i); save the image file as well?

Thanks,
Raj
Yuri
Posts: 64
Joined: Wed Jul 23, 2008 2:47 pm

Re: Adding and Merging Annotations

Post by Yuri »

Hi Raj,

annotations.MergeImageWithAnnotations() does not save the image. It just merges each image from the Images collection with a set of annotations.

For saving image after you should execute save image method. Please refer to "Programming > Save image > Save image collection" section in the SDK documentation here:
http://www.vintasoft.com/docs/vsimaging-dotnet/


Sincerely, Yuri
mrajkumar
Posts: 3
Joined: Tue Apr 23, 2013 3:26 pm

Re: Adding and Merging Annotations

Post by mrajkumar »

Thanks Yuri for a quick response, however I have few more questions.
It looks like there is no single method call for saving the whole image collection in one go. Do I need to keep track of all the image file names and call the Save(filename) method? If I try this, I am getting exception 'This file is being used by another process', no matter if I set SaveAndSwitchStore to true or false. Please see my code sample below

Code: Select all

using (ImageCollection images = new ImageCollection())
{
    foreach (var file in files)
    {
        images.Add(file);
    }
    var cont = new AnnotationController(images);
    images.SetSaveAnnotations(true);
    images.SaveAndSwitchSource = false;
    var encoder = new TiffEncoder();

    // for each image in image collection
    for (int i = 0; i < images.Count; i++)
    {
        cont.MergeImageWithAnnotations(i);
    }
    //save the images
    foreach (var file in files)
    {
        images.SaveAsync(file);
        //images.SaveSync(file);
    }

    // images.ClearAndDisposeItems();
}
DarkStar
Posts: 2
Joined: Sat Jun 13, 2009 4:26 pm

Re: Adding and Merging Annotations

Post by DarkStar »

Hi Raj, try use this code:

Code: Select all

void MergeImagesWithAnnotations(string filename)
{
    using (Stream sourceStream = File.Open(filename, FileMode.Open, FileAccess.ReadWrite))
    {
        ImageCollection images = new ImageCollection();
        images.Add(sourceStream);

        AnnotationController controller = new AnnotationController(images);

        for (int i = 0; i < images.Count; i++)
        {
            // add test annotation
            TextAnnotation text = new TextAnnotation();
            text.Size = new SizeF(100, 100);
            text.Location = new PointF(images[i].Width / 2, images[i].Height / 2);
            text.Text = "Test";
            controller.GetAnnotations(i).Add(text);
        }

        for (int i = 0; i < images.Count; i++)
            controller.MergeImageWithAnnotations(i);
        
        EncoderBase encoder = AvailableEncoders.CreateEncoder(filename);
        if (encoder is MultipageEncoderBase)
        {
            images.SaveAndSwitchSource = true;
            images.SaveSync(sourceStream, (MultipageEncoderBase)encoder);
        }
        else
        {
            encoder.SaveAndSwitchSource = true;
            images[0].Save(sourceStream, encoder);
        }

        images.ClearAndDisposeItems();
    }
}

void MergeImageFilesWithAnnotations(string[] filenames)
{
    foreach (string filename in filenames)
        MergeImagesWithAnnotations(filename);
}
mrajkumar
Posts: 3
Joined: Tue Apr 23, 2013 3:26 pm

Re: Adding and Merging Annotations

Post by mrajkumar »

Many Thanks DarkStar, It works!
Post Reply