Draw and Edit Annotations on DICOM - WinForms C# .NET 6

This tutorial shows how to add annotations to a medical viewer control's frames using actions.

Overview  
Summary This tutorial covers how to add annotations to a medical viewer in a WinForms C# Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (7 KB)
Platform C# .NET 6 WinForms Application
IDE Visual Studio 2022
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and the Load and Display a DICOM Image in the Medical Viewer tutorials, Before working on the Load and Display a DICOM Image in the Medical Viewer - WinForms C# tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Load and Display a DICOM Image in the Medical Viewer tutorial. If the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

Ensure the project has the necessary LEADTOOLS references mentioned in the original tutorial.

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the Annotation Menu Items

With the project created, the references added, the license set, and the Document Viewer code added, coding can begin.

Open Form1.cs in the Designer then add the following to the menu bar:

Set the Visible property for the Save Annotations, Load Annotations, and Annotation menus of these to be false so that they only show after a valid DICOM dataset is loaded in the medical viewer.

Use the code below for the EnableAnnotationMenuItems() function which will make these controls visible after an image is loaded.

Call the EnableAnnotationMenuItems() function after the MedicalViewerMultiCell is initialized and added to the Medical Viewer control in the loadDICOMToolStripMenuItem_Click() method.

C#
private void loadDICOMToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      OpenFileDialog dlg = new OpenFileDialog(); 
      dlg.InitialDirectory = @"C:\LEADTOOLS23\Resources\Images\DICOM"; 
      dlg.Filter = "DICOM DataSets (*.dcm)|*.dcm"; 
      if (dlg.ShowDialog(this) == DialogResult.OK) 
      { 
         _dataSet.Load(dlg.FileName, DicomDataSetLoadFlags.LoadAndClose); 
 
         InitializeMultiCell(); 
 
         if (_cell != null) 
         { 
            SetTags(); 
 
            if (_medicalViewer.Cells.Count > 0) 
               _medicalViewer.Cells.Clear(); 
 
            _medicalViewer.Cells.Add(_cell); 
 
            // Enable annotation menu items after load 
            EnableAnnotationMenuItems(); 
         } 
      } 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 
C#
private void EnableAnnotationMenuItems() 
{ 
   saveAnnotationsToolStripMenuItem.Visible = true; 
   loadAnnotationsToolStripMenuItem.Visible = true; 
   annotationsToolStripMenuItem.Visible = true; 
} 

Add the Annotation Action to the Medical Viewer Cell

Modify the SetActions() to add the three annotation actions to the medical viewer cell when it is initialized.

C#
private void SetActions() 
{ 
   // Keep original code as is 
 
   // Add annotation actions 
   _cell.AddAction(MedicalViewerActionType.AnnotationEllipse); 
   _cell.AddAction(MedicalViewerActionType.AnnotationRuler); 
   _cell.AddAction(MedicalViewerActionType.AnnotationText); 
} 

Add the Draw Annotation Action Code

Add the following to the global variables:

C#
private MedicalViewerActionType _actionType; 
private MedicalViewerAnnotation _ann; 

Click the Events icon in the Properties Windows. Then, double-click the Load event for each of the menu items in the Annotations menu to create an event handler.

Use the following code for the event handlers.

C#
private void circleToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      _actionType = MedicalViewerActionType.AnnotationEllipse; 
      _ann = new MedicalViewerAnnotationEllipse() { AnnotationColor = Color.Blue }; 
 
      DrawAnnotationAction(_actionType, _ann); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 
 
private void rulerToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      _actionType = MedicalViewerActionType.AnnotationRuler; 
      _ann = new MedicalViewerAnnotationRuler() { AnnotationColor = Color.Yellow }; 
 
      DrawAnnotationAction(_actionType, _ann); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 
 
private void textToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      _actionType = MedicalViewerActionType.AnnotationText; 
      _ann = new MedicalViewerAnnotationText() { AnnotationColor = Color.Red }; 
 
      DrawAnnotationAction(_actionType, _ann); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

Use the following code to set the draw annotation action to the left mouse button:

C#
private void DrawAnnotationAction(MedicalViewerActionType actionType, MedicalViewerAnnotation ann) 
{ 
   MedicalViewerActionFlags flags; 
 
   // Draw on Active Cell 
   flags = _cell.GetActionFlags(actionType); 
   flags |= MedicalViewerActionFlags.Active; 
 
   // Apply properties on all objects of the same type 
   ann.Flags = MedicalViewerAnnotationFlags.AllObjects; 
   _cell.SetActionProperties(actionType, ann); 
 
   // Add draw action to left mouse button 
   _cell.SetAction(actionType, MedicalViewerMouseButtons.Left, flags); 
} 

Add Load Annotation Code

Double-click the Load Annotations menu item to edit its event handler, then add the following code in it:

C#
private void saveAnnotationsToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   SaveFileDialog dlg = new SaveFileDialog(); 
   dlg.InitialDirectory = @"C:\LEADTOOLS23\Resources\Images\DICOM"; 
   dlg.Filter = "LEAD Annotations XML (*.xml)|*.xml"; 
   if (dlg.ShowDialog(this) == DialogResult.OK) 
   { 
      _medicalViewer.Cells[0].SaveAnnotations(dlg.FileName); 
   } 
} 

Add Save Annotation Code

Double-click the Save Annotations menu item to edit its event handler, then add the following code in it:

C#
private void saveAnnotationsToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   SaveFileDialog dlg = new SaveFileDialog(); 
   dlg.InitialDirectory = @"C:\LEADTOOLS23\Resources\Images\DICOM"; 
   dlg.Filter = "LEAD Annotations XML (*.xml)|*.xml"; 
   if (dlg.ShowDialog(this) == DialogResult.OK) 
   { 
      _medicalViewer.Cells[0].SaveAnnotations(dlg.FileName); 
   } 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and allows annotiations to be drawn on the displayed medical image frame. The tutorial also allows saving and loading annotations using an annotations XML file on disk.

Wrap-up

This tutorial showed how to add the necessary references to draw and edit annotations in the MedicalViewer WinForms control using the MedicalViewerMultiCell.SetAction() method. In addition, it showed how to use the MedicalViewer.Cell.LoadAnnotations and MedicalViewer.Cell.SaveAnnotations to load and save annotations using an XML file on disk.

See Also

Help Version 23.0.2024.4.23
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.