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