Bulk load images and annotations

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

Moderator: Alex

Post Reply
iansml
Posts: 31
Joined: Thu Apr 23, 2009 1:57 pm

Bulk load images and annotations

Post by iansml » Mon May 11, 2009 1:50 pm

Hi Alex,

I've written some code that will allow users to select multiple images to upload and silently load the annotations from separate XMP files. It works partially, in that the images load and the annotations are added to the image. The problem I have is that for some reason the ThumbnailViewer only shows the annotations on the first image in the ThumbnailViewer (although if you select the other thumbnail images the annotations are visible in the ImageViewer). Here is my code, any ideas..?

NB. The assumption here is that IMAGE.TIF has a corresponding annotation file called IMAGE.XMP in the same directory.

Code: Select all

private void LoadImagesFromFile() {
            OpenFileDialog openImageDialog = GetOpenImageDialog();
            openImageDialog.Multiselect = true;

            if (openImageDialog.ShowDialog() == DialogResult.OK) {
                foreach (string fileName in openImageDialog.FileNames) {
                    try {
                        //add image
                        thumbnailViewer1.Images.Add(fileName);
                        
                        //focus on newly added image
                        thumbnailViewer1.FocusedIndex = thumbnailViewer1.Images.Count - 1;

                        if (acquireAnnotationsOnImageLoadToolStripMenuItem.Checked) {
                            //load annotations
                            LoadAnnotations(thumbnailViewer1.FocusedIndex, FilenameUtils.ChangeFileExtension(fileName, EXT_XMP));
                        }
                    }
                    catch (Exception ex) {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

private void LoadAnnotations(int imageIndex, string filePath) {
            string xmpFile = FilenameUtils.ChangeFileExtension(filePath, EXT_XMP);

            if (CheckImageSelected() && File.Exists(xmpFile)) {
                int nextAnnoIndex = thumbnailViewer1.Annotations[imageIndex].Count;
                
                //load annotations from file
                XmlDocument doc = new XmlDocument();
                doc.Load(xmpFile);
                thumbnailViewer1.Annotations[imageIndex].Load(doc);

                //update zOrder, adding number of annotations loaded
                zOrder += numAnnotationsLoaded;
            }
        }
I've tried calling Refresh() on the ThumbnailViewer after adding images to try and redraw the control but it didn't do any good. Is it anything to do with how I'm trying to programatically focus on the next image in the ThumbnailViewer? What is the best way to do this when loading multiple images.

Thanks.

Alex
Site Admin
Posts: 1445
Joined: Thu Jul 10, 2008 2:21 pm

Re: Bulk load images and annotations

Post by Alex » Tue May 12, 2009 9:27 am

Hi Ian,

You should subscribe to the CollectionChanged event of the thumbnailViewer1.Images object, wait for the event with collectionChangeEvent = ImageCollectionChangeAction.ImageAdded and only then call the LoadAnnotations method.

Best regards, Alexander

iansml
Posts: 31
Joined: Thu Apr 23, 2009 1:57 pm

Re: Bulk load images and annotations

Post by iansml » Tue May 12, 2009 12:54 pm

Hi Alex,

I refactored the code to call the LoadAnnotations method in the ImagesCollectionChanged event. I'm still having the same problem though - the annotations are visible in the ImageViewer when you select the thumbnail BUT the annotations only show up on the first thumbnail in the thumbnail viewer when loading multiple images and annotations in bulk.

Any idea where I'm going wrong? I'm getting pretty confused now..

Code: Select all


... thumbnailViewer1.Images.CollectionChanged += new ImageCollectionChangeEventHandler(thumbnailImages_CollectionChanged);
       
        private void thumbnailImages_CollectionChanged(object sender, ImageCollectionChangeEventArgs e) {
            if (e.Action == ImageCollectionChangeAction.ImageAdded && acquireAnnotationsOnImageLoadToolStripMenuItem.Checked) {
                //load annotations
                LoadAnnotations(e.Image, FilenameUtils.ChangeFileExtension(e.Image.ImageSourceFilename, EXT_XMP));
            }
        }

        private void LoadImagesFromFile() {
            OpenFileDialog openImageDialog = GetOpenImageDialog();
            openImageDialog.Multiselect = true;

            if (openImageDialog.ShowDialog() == DialogResult.OK) {

                //clear images if required
                if (chkClearImagesToolStripMenuItem.Checked) {
                    thumbnailViewer1.Images.Clear();
                }

                //load images
                foreach (string fileName in openImageDialog.FileNames) {
                    try {
                        //add image
                        thumbnailViewer1.Images.Add(fileName);
                    }
                    catch (Exception ex) {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void LoadAnnotations(VintasoftImage img, string filePath) {
            string xmpFile = FilenameUtils.ChangeFileExtension(filePath, EXT_XMP);
            int imageIndex = GetImageIndex(img);

            if (CheckImageSelected() && File.Exists(xmpFile) && imageIndex >= 0) {
                int nextAnnoIndex = thumbnailViewer1.Annotations[imageIndex].Count;

                //load annotations from file
                XmlDocument doc = new XmlDocument();
                doc.Load(xmpFile);
                thumbnailViewer1.Annotations[imageIndex].Load(doc);

                //update zOrder, adding number of annotations loaded
                zOrder += numAnnotationsLoaded;
            }
        }
       
        private int GetImageIndex(VintasoftImage image) {
            int retVal = -1;
            if (image != null && thumbnailViewer1.Images.Count > 0) {
                retVal = thumbnailViewer1.Images.IndexOf(image);
            }
            return retVal;
        }

Thanks very much,

Ian

Alex
Site Admin
Posts: 1445
Joined: Thu Jul 10, 2008 2:21 pm

Re: Bulk load images and annotations

Post by Alex » Tue May 12, 2009 1:19 pm

Hi Ian,

Please send a working example that demonstrates the problem to support@vintasoft.com.

Best regards, Alexander

iansml
Posts: 31
Joined: Thu Apr 23, 2009 1:57 pm

Re: Bulk load images and annotations

Post by iansml » Tue May 12, 2009 3:43 pm

Alex.

Sent a ZIP project to your support team. How long should it take for your guys to take a look at it?

Thanks,

Ian

Alex
Site Admin
Posts: 1445
Joined: Thu Jul 10, 2008 2:21 pm

Re: Bulk load images and annotations

Post by Alex » Wed May 13, 2009 2:55 pm

Hi Ian,

Problem is fixed.

Best regards, Alexander

iansml
Posts: 31
Joined: Thu Apr 23, 2009 1:57 pm

Re: Bulk load images and annotations

Post by iansml » Wed May 13, 2009 3:19 pm

Alex,

Thanks very much. Dowloaded v2.2.2.1 and it works great. Very quick turnaround. Good work..

Ian

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests