Load and Display a DICOM Image in the Medical Viewer - WinForms C#

This tutorial shows how to load and display a DICOM image in the LEADTOOLS MedicalViewer WinForms control. This will also show how to load DICOM tags for display in the viewer control, as well as how to configure viewer display actions with mouse and keyboard.

Overview  
Summary This tutorial covers how to load and display a DICOM image in a WinForms C# Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (11 KB)
Platform Windows WinForms C# Application
IDE Visual Studio 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, 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

In Visual Studio, create a new C# Windows WinForms project, and add the below necessary LEADTOOLS references.

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:

If NuGet references are used, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

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:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License - HTML5 JavaScript tutorial.

Initialize the MedicalViewer

Now that the LEADTOOLS references have been added to the project and the license has been set, coding can begin.

Right-click on Form1.cs in the Solution Explorer and select View Code to display the code behind the form. Add the below code to initialize the Medical Viewer control.

C#
// Using block at the top  
using System; 
using System.IO; 
using System.Windows.Forms; 
using Leadtools; 
using Leadtools.Dicom; 
using Leadtools.MedicalViewer; 
C#
// Add these global variables  
private DicomDataSet _dataSet; 
private MedicalViewer _medicalViewer; 
private MedicalViewerMultiCell _cell; 
private RasterImage _image; 

In Solution Explorer, double-click Form1.cs to display it in the Designer. Click the Events icon in the Properties Window. Then, double-click the Load event to create an event handler if one does not already exist.

Add the following code inside the Form1_Load event handler.

C#
private void Form1_Load(object sender, EventArgs e)  
{  
   _medicalViewer = new MedicalViewer(1, 1); 
   _medicalViewer.Dock = DockStyle.Fill; 
   Controls.Add(_medicalViewer); 
 
   DicomEngine.Startup(); 
   _dataSet = new DicomDataSet(); 
}  

Add the Load DICOM Image Code

Open Form1.cs in the Designer, then add a File menu with an Load DICOM menu item. To do that, open the Toolbox and double-click MenuStrip, which will add a menu to the form. In the Designer, change the text of the menu to &File. Then add an item to the menu and set its text to &Load DICOM. Leave the new item's name as loadDICOMToolStripMenuItem.

Double-click the Load DICOM menu item to edit its event handler. Add the following code in it:

C#
private void loadDICOMToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      OpenFileDialog dlg = new OpenFileDialog(); 
      dlg.InitialDirectory = @"C:\LEADTOOLS22\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); 
         } 
      } 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

Use the following code for the InitializeMultiCell() method. This method will create an instance of the MedicalViewerMultiCell class using the image from the DICOM file as well as set the cell's properties, including enabling the display of rulers and tags.

C#
private void InitializeMultiCell() 
{ 
   int dataSet_TotalPages = 0; 
 
   DicomElement pixelDataElement = _dataSet.FindFirstElement(null, DicomTag.PixelData, false); 
   if (pixelDataElement != null) 
   { 
      dataSet_TotalPages = _dataSet.GetImageCount(pixelDataElement); 
      if (dataSet_TotalPages != 0) 
      { 
         // Get image(s) from DICOM file 
         _image = _dataSet.GetImages(pixelDataElement, 0, dataSet_TotalPages, 0, RasterByteOrder.Gray, DicomGetImageFlags.AutoApplyModalityLut | DicomGetImageFlags.AutoApplyVoiLut); 
 
         // Initialize Medical Viewer MultiCell 
         _cell = new MedicalViewerMultiCell(_image, true, 1, 1); 
 
         _cell.Rows = 1; 
         _cell.Columns = 1; 
         _cell.Frozen = false; 
         _cell.DisplayRulers = MedicalViewerRulers.Both; 
         _cell.ApplyOnIndividualSubCell = false; 
         _cell.ApplyActionOnMove = true; 
         _cell.FitImageToCell = true; 
         _cell.Selected = true; 
         _cell.ShowTags = true; 
 
         // Set Mouse and Keyboard actions 
         SetActions(); 
      } 
      else 
      { 
         MessageBox.Show("This DataSet's Pixel Data Element does not contain any image data"); 
         _cell = null; 
      } 
   } 
   else 
   { 
      MessageBox.Show("This DataSet does not contain a Pixel Data Element"); 
      _cell = null; 
   } 
} 

Use the following code for the SetActions() method that will add keyboard and mouse actions to control the display of the DICOM image in the medical viewer.

C#
private void SetActions() 
{ 
   // Add some actions that will be used to change the properties of the images inside the control.  
   _cell.AddAction(MedicalViewerActionType.WindowLevel); 
   _cell.AddAction(MedicalViewerActionType.Offset); 
   _cell.AddAction(MedicalViewerActionType.Stack); 
 
   // Assign the added actions to a mouse button, meaning that when the user clicks and drags the mouse button, the associated action will be activated.  
   _cell.SetAction(MedicalViewerActionType.Offset, MedicalViewerMouseButtons.Right, MedicalViewerActionFlags.Active); 
   _cell.SetAction(MedicalViewerActionType.WindowLevel, MedicalViewerMouseButtons.Left, MedicalViewerActionFlags.Active); 
   _cell.SetAction(MedicalViewerActionType.Stack, MedicalViewerMouseButtons.Wheel, MedicalViewerActionFlags.Active); 
 
   // Assign the added actions to a keyboard keys that will work like the mouse.  
   MedicalViewerKeys medicalKeys = new MedicalViewerKeys(Keys.Down, Keys.Up, Keys.Left, Keys.Right, MedicalViewerModifiers.None); 
   _cell.SetActionKeys(MedicalViewerActionType.Offset, medicalKeys); 
   medicalKeys.Modifiers = MedicalViewerModifiers.Ctrl; 
   _cell.SetActionKeys(MedicalViewerActionType.WindowLevel, medicalKeys); 
} 

Add the DICOM Tags Code

Use the code below for the SetTags() method which will read the tag information from the DICOM dataset and display them in the cell.

C#
private void SetTags() 
{ 
   // Extract tags from DICOM file 
   string dpatientID = GetDicomTag(_dataSet, DicomTag.PatientID); 
   string dpatientName = GetDicomTag(_dataSet, DicomTag.PatientName); 
   string dpatientAge = GetDicomTag(_dataSet, DicomTag.PatientAge); 
   string dpatientBirthDate = GetDicomTag(_dataSet, DicomTag.PatientBirthDate); 
   string dpatientSex = GetDicomTag(_dataSet, DicomTag.PatientSex); 
 
   // Set tags in MultiCell 
   _cell.SetTag(1, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, "Name: " + dpatientName); 
   _cell.SetTag(2, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, "ID: " + dpatientID); 
   _cell.SetTag(3, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, "DOB: " + dpatientBirthDate); 
   _cell.SetTag(4, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, "Age: " + dpatientAge); 
   _cell.SetTag(5, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, "Sex: " + dpatientSex); 
 
   _cell.SetTag(4, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Frame); 
   _cell.SetTag(6, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Scale); 
   _cell.SetTag(2, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.WindowLevelData); 
   _cell.SetTag(1, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.FieldOfView); 
   _cell.SetTag(0, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.RulerUnit); 
} 
C#
private string GetDicomTag(DicomDataSet ds, long tag) 
{ 
   DicomElement patientElement = ds.FindFirstElement(null, tag, true); 
 
   if (patientElement != null) 
      return ds.GetConvertValue(patientElement); 
 
   return null; 
} 

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 loads and displays a selected DICOM image in the MedicalViewer control while also displaying the designated tags contained in the DICOM file. The application will also allow controlling the display using mouse and keyboard actions.

Wrap-up

This tutorial showed how to add the necessary references to load and display a DICOM image in the MedicalViewer WinForms control. In addition, it showed how to use the MedicalViewerMultiCell object.

See Also

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

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