/// <summary> /// Loads the measurement annotations from a file. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void loadMeasurementsAction_Clicked(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.FileName = null; openFileDialog.Filter = "Binary Annotations(*.vsabm)|*.vsabm|XMP Annotations(*.xmpm)|*.xmpm|All Formats(*.vsabm;*.xmpm)|*.vsabm;*.xmpm"; openFileDialog.FilterIndex = 3; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) { ImageMeasureTool visualTool = (ImageMeasureTool)VisualTool; // get the annotation collection AnnotationDataCollection annotations = visualTool.AnnotationViewCollection.DataCollection; // clear the annotation collection annotations.ClearAndDisposeItems(); // add annotations from stream to the annotation collection annotations.AddFromStream(fs, visualTool.ImageViewer.Image.Resolution); } } catch (Exception ex) { DemosTools.ShowErrorMessage(ex); } } } }
/// <summary> /// Saves annotation collection to a file. /// </summary> /// <param name="annotationViewer">The annotation viewer.</param> /// <param name="saveFileDialog">The save file dialog.</param> public static void SaveAnnotationsToFile(AnnotationViewer annotationViewer, SaveFileDialog saveFileDialog) { // cancel annotation building annotationViewer.CancelAnnotationBuilding(); saveFileDialog.FileName = null; saveFileDialog.Filter = "Binary Annotations|*.vsab|XMP Annotations|*.xmp|WANG Annotations|*.wng"; saveFileDialog.FilterIndex = 1; if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { try { // open specified file using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.ReadWrite)) { // get annotation formatter IFormatter formatter = null; if (saveFileDialog.FilterIndex == 1) { formatter = new AnnotationVintasoftBinaryFormatter(); } else if (saveFileDialog.FilterIndex == 2) { formatter = new AnnotationVintasoftXmpFormatter(); } else if (saveFileDialog.FilterIndex == 3) { if (MessageBox.Show( "Important: some data from annotations will be lost. Do you want to continue anyway?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) { return; } formatter = new AnnotationWangFormatter(annotationViewer.Image.Resolution); } // get focused annotation data collection AnnotationDataCollection annotations = annotationViewer.AnnotationDataController[annotationViewer.FocusedIndex]; // serialize annotation data to specified stream formatter.Serialize(fs, annotations); } } catch (Exception ex) { DemosTools.ShowErrorMessage(ex); } } }
/// <summary> /// Burns the annotations on PDF page with vector graphics. /// </summary> /// <param name="annotations">The annotations.</param> /// <param name="resolution">The resolution.</param> /// <param name="page">The page.</param> private static void BurnPdfAnnotations( AnnotationDataCollection annotations, Resolution resolution, PdfPage page) { PdfAnnotation[] annots = AnnotationConverter.ConvertToPdfAnnotations(annotations, resolution, page); using (PdfGraphics g = page.GetGraphics()) foreach (PdfAnnotation annot in annots) { g.DrawAnnotation(annot); } }
/// <summary> /// The annotation data property is changed. /// </summary> /// <param name="annotationDataCollection">The annotation data collection.</param> /// <param name="annotationData">The annotation data.</param> /// <param name="e">The <see cref="ObjectPropertyChangedEventArgs" /> instance containing the event data.</param> protected override void OnAnnotationDataPropertyChanged( AnnotationDataCollection annotationDataCollection, AnnotationData annotationData, ObjectPropertyChangedEventArgs e) { IRuler ruler = annotationData as IRuler; if (ruler != null) { if (e.PropertyName == "UnitOfMeasure") { StringBuilder formatString = new StringBuilder("0.0 "); switch (ruler.UnitOfMeasure) { case UnitOfMeasure.Inches: formatString.Append("in"); break; case UnitOfMeasure.Centimeters: formatString.Append("cm"); break; case UnitOfMeasure.Millimeters: formatString.Append("mm"); break; case UnitOfMeasure.Pixels: formatString.Append("px"); break; case UnitOfMeasure.Points: formatString.Append("point"); break; case UnitOfMeasure.DeviceIndependentPixels: formatString.Append("dip"); break; case UnitOfMeasure.Twips: formatString.Append("twip"); break; case UnitOfMeasure.Emu: formatString.Append("emu"); break; } ruler.FormatString = formatString.ToString(); } } }
/// <summary> /// The annotation data collection is changed. /// </summary> private void dataCollection_Changed(object sender, CollectionChangeEventArgs <AnnotationData> e) { // if annotation has been added to the collection if (e.Action == CollectionChangeActionType.InsertItem || e.Action == CollectionChangeActionType.SetItem) { AnnotationDataCollection dataCollection = (AnnotationDataCollection)sender; AnnotationViewCollection viewCollection = _annotationViewer.AnnotationViewController.GetAnnotationsView(dataCollection); // subscribe to the annotation view events SubscribeToAnnotationViewEvents(viewCollection.FindView(e.NewValue)); } if (_isEnabled) { OnDataCollectionChanged((AnnotationDataCollection)sender, e); } }
/// <summary> /// Groups all annotations of annotation collection. /// </summary> /// <param name="annotationViewer">The annotation viewer.</param> /// <param name="undoManager">The undo manager.</param> public static void GroupAllAnnotations(AnnotationViewer annotationViewer, CompositeUndoManager undoManager) { // cancel the annotation building annotationViewer.CancelAnnotationBuilding(); // get reference to the annotation collection of focused image AnnotationDataCollection annotationDataCollection = annotationViewer.AnnotationDataController[annotationViewer.FocusedIndex]; if (annotationDataCollection.Count == 0) { return; } // begin the composite undo action undoManager.BeginCompositeAction("Group all annotations"); try { // save the references to annotations in array AnnotationData[] annotationDataArray = annotationDataCollection.ToArray(); // clear the annotation collection of focused image annotationDataCollection.Clear(); // create the group annotation GroupAnnotationData groupAnnotationData = new GroupAnnotationData(); // for each annotation in array for (int i = 0; i < annotationDataArray.Length; i++) { // add annotations from array to group annotation groupAnnotationData.Items.Add(annotationDataArray[i]); } // add the group annotation to the annotation collection of focused image annotationDataCollection.Add(groupAnnotationData); annotationViewer.FocusedAnnotationData = groupAnnotationData; } finally { // end the composite undo action undoManager.EndCompositeAction(); } }
/// <summary> /// The annotation data property is changed. /// </summary> /// <param name="annotationDataCollection">The annotation data collection.</param> /// <param name="annotationData">The annotation data.</param> /// <param name="e">The <see cref="ObjectPropertyChangedEventArgs"/> instance containing the event data.</param> protected override void OnAnnotationDataPropertyChanged( AnnotationDataCollection annotationDataCollection, AnnotationData annotationData, ObjectPropertyChangedEventArgs e) { if (e.OldValue == null && e.NewValue == null) { AddLogMessage(string.Format("{0}.{1}", GetAnnotationInfo(annotationData), e.PropertyName)); } else { AddLogMessage(string.Format("{0}.{1}: {2} -> {3}", GetAnnotationInfo(annotationData), e.PropertyName, e.OldValue, e.NewValue)); } }
/// <summary> /// Unsubscribes from <see cref="AnnotationDataCollection"/> events. /// </summary> /// <param name="dataCollection">The annotation data collection.</param> private void UnubscribeFromAnnotationDataCollectionEvents(AnnotationDataCollection dataCollection) { if (_handleViewEvents) { AnnotationViewCollection viewCollection = _annotationViewer.AnnotationViewController.GetAnnotationsView(dataCollection); foreach (AnnotationView view in viewCollection) { UnsubscribeFromAnnotationViewEvents(view); } } if (_handleDataEvents) { dataCollection.ItemPropertyChanging -= new EventHandler <AnnotationDataPropertyChangingEventArgs>(dataCollection_ItemPropertyChanging); dataCollection.ItemPropertyChanged -= new EventHandler <AnnotationDataPropertyChangedEventArgs>(dataCollection_ItemPropertyChanged); } dataCollection.Changing -= new CollectionChangeEventHandler <AnnotationData>(dataCollection_Changing); dataCollection.Changed -= new CollectionChangeEventHandler <AnnotationData>(dataCollection_Changed); }
/// <summary> /// The annotation data collection is changed. /// </summary> /// <param name="annotationDataCollection">The annotation data collection.</param> /// <param name="e">The <see cref="CollectionChangeEventArgs{AnnotationData}"/> instance containing the event data.</param> protected override void OnDataCollectionChanged( AnnotationDataCollection annotationDataCollection, CollectionChangeEventArgs <AnnotationData> e) { if (e.NewValue != null && e.OldValue != null) { AddLogMessage(string.Format("DataCollection.{0}: {1}->{2}", e.Action, GetAnnotationInfo(e.OldValue), GetAnnotationInfo(e.NewValue))); } else if (e.NewValue != null) { AddLogMessage(string.Format("DataCollection.{0}: {1}", e.Action, GetAnnotationInfo(e.NewValue))); } else if (e.OldValue != null) { AddLogMessage(string.Format("DataCollection.{0}: {1}", e.Action, GetAnnotationInfo(e.OldValue))); } else { AddLogMessage(string.Format("DataCollection.{0}", e.Action)); } }
/// <summary> /// Saves the measurement annotations to a file. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void saveMeasurementsAction_Clicked(object sender, EventArgs e) { using (SaveFileDialog saveFileDialog = new SaveFileDialog()) { saveFileDialog.FileName = null; saveFileDialog.Filter = "Binary Annotations|*.vsabm|XMP Annotations|*.xmpm"; saveFileDialog.FilterIndex = 1; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.ReadWrite)) { IFormatter formatter = null; if (saveFileDialog.FilterIndex == 1) { formatter = new AnnotationVintasoftBinaryFormatter(); } else if (saveFileDialog.FilterIndex == 2) { formatter = new AnnotationVintasoftXmpFormatter(); } ImageMeasureTool visualTool = (ImageMeasureTool)VisualTool; AnnotationDataCollection annotations = visualTool.AnnotationViewCollection.DataCollection; formatter.Serialize(fs, annotations); } } catch (Exception ex) { DemosTools.ShowErrorMessage(ex); } } } }
/// <summary> /// Loads annotation collection from file. /// </summary> /// <param name="annotationViewer">The annotation viewer.</param> /// <param name="openFileDialog">The open file dialog.</param> /// <param name="undoManager">The undo manager.</param> public static void LoadAnnotationsFromFile(AnnotationViewer annotationViewer, OpenFileDialog openFileDialog, CompositeUndoManager undoManager) { // cancel annotation building annotationViewer.CancelAnnotationBuilding(); openFileDialog.FileName = null; openFileDialog.Filter = "Binary Annotations(*.vsab)|*.vsab|XMP Annotations(*.xmp)|*.xmp|WANG Annotations(*.wng)|*.wng|All Formats(*.vsab;*.xmp;*.wng)|*.vsab;*.xmp;*.wng"; openFileDialog.FilterIndex = 4; if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // begin the composite undo action undoManager.BeginCompositeAction("Load annotations from file"); try { using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) { // get the annotation collection AnnotationDataCollection annotations = annotationViewer.AnnotationDataCollection; // clear the annotation collection annotations.ClearAndDisposeItems(); // add annotations from stream to the annotation collection annotations.AddFromStream(fs, annotationViewer.Image.Resolution); } } catch (Exception ex) { DemosTools.ShowErrorMessage(ex); } finally { // end the composite undo action undoManager.EndCompositeAction(); } } }
/// <summary> /// The annotation data property is changed. /// </summary> /// <param name="annotationDataCollection">The annotation data collection.</param> /// <param name="annotationData">The annotation data.</param> /// <param name="e">The <see cref="ObjectPropertyChangedEventArgs"/> instance containing the event data.</param> protected virtual void OnAnnotationDataPropertyChanged( AnnotationDataCollection annotationDataCollection, AnnotationData annotationData, ObjectPropertyChangedEventArgs e) { }
/// <summary> /// The annotation data controller is changed. /// </summary> /// <param name="annotationDataCollection">The annotation data collection.</param> /// <param name="e">The <see cref="CollectionChangeEventArgs{AnnotationData}"/> instance containing the event data.</param> protected virtual void OnDataCollectionChanged(AnnotationDataCollection annotationDataCollection, CollectionChangeEventArgs <AnnotationData> e) { }