This tutorial shows how to load and display a DICOM image in the LEADTOOLS MedicalViewer
WinForms control. This will also show how to load and display DICOM tags in the viewer control, as well as how to configure mouse and keyboard viewer display actions.
Overview | |
---|---|
Summary | This tutorial covers how to load and display a DICOM image in the Medical Viewer 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, 2022 |
Development License | Download LEADTOOLS |
Try it in another language |
|
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.
Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, 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).
If using NuGet references, this tutorial requires the following NuGet package:
Leadtools.Medical.Viewer.WinForms
Leadtools.Jpeg2000
If using local DLL references, the following DLLs are needed.
The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.J2k.dll
Leadtools.Dicom.dll
Leadtools.MedicalViewer.dll
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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:
With the project created, the references added, and the license 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 code below to the using
block at the top.
using System;
using System.IO;
using System.Windows.Forms;
using Leadtools;
using Leadtools.Dicom;
using Leadtools.MedicalViewer;
Add the global variables below to initialize the MedicalViewer
control and the current multi-cell component.
private MedicalViewer _medicalViewer;
private MedicalViewerMultiCell _cell;
In the 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.
private void Form1_Load(object sender, EventArgs e)
{
// Create the MedicalViewer control and add it to the form
_medicalViewer = new MedicalViewer(1, 1);
_medicalViewer.Dock = DockStyle.Fill;
Controls.Add(_medicalViewer);
// Startup the DICOM engine
DicomEngine.Startup();
}
Open Form1.cs
in the Designer, then add a File menu with a 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:
private void loadDICOMToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
// Clear existing cells
if (_medicalViewer.Cells.Count > 0)
_medicalViewer.Cells.Clear();
// Show Open File dialog
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images\DICOM";
dlg.Filter = "DICOM DataSets (*.dcm)|*.dcm";
if (dlg.ShowDialog(this) == DialogResult.OK)
{
DicomDataSet dataSet = new DicomDataSet();
dataSet.Load(dlg.FileName, DicomDataSetLoadFlags.LoadAndClose);
DicomElement pixelDataElement = dataSet.FindFirstElement(null, DicomTag.PixelData, false);
if (pixelDataElement != null)
{
// Get images from DICOM dataset
DicomGetImageFlags dicomGetImageFlags = DicomGetImageFlags.AutoDetectInvalidRleCompression | DicomGetImageFlags.AutoApplyModalityLut | DicomGetImageFlags.AutoApplyVoiLut;
RasterImage image = dataSet.GetImages(pixelDataElement, 0, dataSet.GetImageCount(pixelDataElement), 0, RasterByteOrder.Gray, dicomGetImageFlags);
// Create viewer cell using the loaded images
InitializeMultiCell(image);
if (_cell != null)
{
// Add the initialized multi-cell
_medicalViewer.Cells.Add(_cell);
}
}
else
{
MessageBox.Show("This dataset does not contain a PixelData element");
_cell = null;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Create a new method named InitializeMultiCell
method. This method will be called inside the loadDICOMToolStripMenuItem_Click
event handler, as shown above. Add the code below to create an instance of the MedicalViewerMultiCell
class using the image from the DICOM file and set the cell's properties and actions.
private void InitializeMultiCell(RasterImage image)
{
// 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
// 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);
}
Modify the previously added code in the loadDICOMToolStripMenuItem_Click
event handler to obtain the user data tag information from the loaded dataset.
// Create viewer cell using the loaded images
InitializeMultiCell(image);
if (_cell != null)
{
// Get user data to display as tags on viewer
string[] userDataTags = new string[6];
userDataTags[0] = GetDicomTag(dataSet, DicomTag.InstitutionName);
userDataTags[1] = GetDicomTag(dataSet, DicomTag.PatientName);
userDataTags[2] = GetDicomTag(dataSet, DicomTag.PatientAge);
userDataTags[3] = GetDicomTag(dataSet, DicomTag.PatientBirthDate);
userDataTags[4] = GetDicomTag(dataSet, DicomTag.PatientSex);
userDataTags[5] = GetDicomTag(dataSet, DicomTag.PatientID);
SetTags(userDataTags);
// Add the initialized multi-cell
_medicalViewer.Cells.Add(_cell);
}
Add a new method named GetDicomTag
, which will be called above to retrieve each dicom tag. Add the code below:
private string GetDicomTag(DicomDataSet ds, long tag)
{
DicomElement element = ds.FindFirstElement(null, tag, true);
if (element != null)
return ds.GetConvertValue(element);
return null;
}
Add a new SetTags
method which will be called inside the loadDICOMToolStripMenuItem_Click
event handler, as shown above. Add the code below to configure the tag information to be shown on the viewer's frames using user-provided data and the properties of the currently displayed frame.
private void SetTags(string[] userDataTags)
{
// Set user data tags
_cell.SetTag(2, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, userDataTags[0]);
_cell.SetTag(3, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, userDataTags[1]);
_cell.SetTag(4, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, userDataTags[2]);
_cell.SetTag(5, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, userDataTags[3]);
_cell.SetTag(6, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, userDataTags[4]);
_cell.SetTag(7, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, userDataTags[5]);
// Set other tags
_cell.SetTag(6, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Scale);
_cell.SetTag(7, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Frame);
_cell.SetTag(0, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.RulerUnit);
_cell.SetTag(1, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.FieldOfView);
_cell.SetTag(2, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.WindowLevelData);
_cell.SetTag(4, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.Alpha);
}
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.
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.