Navigate DICOM Elements - Console C#

This tutorial shows how to use the DicomDataSet class to find DICOM dataset elements in a C# Windows Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use methods to find DICOM elements in a C# Windows Console application.
Completion Time 40 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
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 tutorial, before working on the Navigate DICOM Elements - Console C# tutorial.

Also, we recommend reviewing the DICOM basic topic of Working with Data Sets.

Download Sample Data Set

For testing the code in this tutorial, download and extract this sample DICOM data set file, which contains the elements shown in the image below. The dataset contains 3 Code Value elements, which are highlighted in the image and their values are shown. The tutorial's code will be searching for 2 particular Code Value elements that lie under the Anatomic Region Sequence element.

Contents of Sample Dataset

Create the Project and Add LEADTOOLS References

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:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\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

How to properly add LEADTOOLS NuGet and local references is covered in the Add References and Set a License tutorial.

Add the Find First Element Code

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

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs:

C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Dicom; 

Add the below global DicomDataSet variable inside the Program class.

C#
static DicomDataSet dataset = null; 

Add two new methods named FindFirstElementTest(DicomElement parent) and FindFirstDescendantTest(DicomElement parent). These methods are called inside the Main(), as shown in the bottom section. Add the code below to the new methods.

Note

Both methods will locate the first Code Value element that is under the Anatomic Region Sequence element. But the code example shows that using the FindFirstDescendant method is a straightforward approach in this particular case and the better option than using the FindFirstElement method.


C#
static DicomElement FindFirstElementTest(DicomElement parent) 
{ 
   Console.Write("FindFirstElement - "); 
   DicomElement child1 = dataset.GetChildElement(parent, true); 
   if (child1 != null && child1.Tag == DicomTag.Item) 
   { 
      DicomElement grandchild = dataset.GetChildElement(child1, true); 
      if (grandchild != null) 
      { 
         DicomElement element = dataset.FindFirstElement(grandchild, DicomTag.CodeValue, true); 
         if (element == null) 
            Console.WriteLine("Unable to find first element"); 
         else 
            Console.WriteLine(dataset.GetValue<string>(element, "Unable to get value")); 
         return element; 
      } 
   } 
   return null; 
} 
C#
static DicomElement FindFirstDescendantTest(DicomElement parent) 
{ 
   Console.Write("FindFirstDescendant A - "); 
   DicomElement child = dataset.FindFirstDescendant(parent, DicomTag.CodeValue, false); 
   if (child != null) 
      Console.WriteLine(dataset.GetValue<string>(child, "Unable to get value")); 
   else 
      Console.WriteLine("Unable to find Descendant"); 
 
   return child; 
} 

Add the Find Next Element Code

Add two new methods named FindNextElementTest(DicomElement parent, DicomElement firstElement) and FindNextDescendantTest(DicomElement parent, DicomElement descendant). These methods will be called inside the Main(). Add the code below to the new methods.

Note

Both methods will locate the second Code Value element that is under the Anatomic Region Sequence element, but the code will show that using the FindNextDescendant method will be easier in this particular case than using the FindNextElement method.


C#
static void FindNextElementTest(DicomElement parent, DicomElement firstElement) 
{ 
   Console.Write("FindNextElement - "); 
   if (parent != null) 
   { 
      DicomElement child1 = dataset.GetChildElement(parent, true); 
      if (child1 != null && child1.Tag == DicomTag.Item) 
      { 
         DicomElement grandchild = dataset.GetChildElement(child1, true); 
         if (grandchild != null) 
         { 
            DicomElement element = dataset.FindNextElement(firstElement, false); 
            if (element != null) 
               Console.WriteLine(dataset.GetValue<string>(element, "Unable to get value")); 
            else 
               Console.WriteLine("Unable to find next element"); 
         } 
      } 
   } 
} 
C#
static void FindNextDescendantTest(DicomElement parent, DicomElement descendant) 
{ 
   Console.Write("FindNextDescendant - "); 
   if (parent != null) 
   { 
      DicomElement child = dataset.FindNextDescendant(parent, descendant, false); 
      if (child != null) 
         Console.WriteLine(dataset.GetValue<string>(child, "Unable to get value")); 
      else 
         Console.WriteLine("Unable to find Next Descendant"); 
   } 
} 

Complete the Main code

Add the code below to initialize and start-up the DicomEngine and call the four methods created above.

C#
static void Main(string[] args) 
{ 
   SetLicense(); 
 
   string filename = @"SampleDataSet.dcm"; //Set the full path and filename of the sample data set 
   DicomEngine.Startup(); 
   dataset = new DicomDataSet(); 
   dataset.Load(filename, DicomDataSetLoadFlags.LoadAndClose); 
 
   // Locate the parent 'Anatomic Region Sequence' element 
   DicomElement parent = dataset.FindFirstElement(null, DicomTag.AnatomicRegionSequence, true); 
   if (parent == null) 
   { 
      Console.WriteLine("Could not find parent 'Anatomic Region Sequence' element"); 
      return; 
   } 
 
   // Use FirstElement/NextElement methods 
   DicomElement firstElement = FindFirstElementTest(parent); 
   FindNextElementTest(parent, firstElement); 
 
   // Use FirstDescendant/NextDescendant methods 
   DicomElement descendant = FindFirstDescendantTest(parent); 
   FindNextDescendantTest(parent, descendant); 
 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

Run the Project

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

If the steps were followed correctly, the application will locate both of the two desired DICOM elements in two different ways and output their values as shown in the following image:

The application runs and displays the element values

Wrap-up

This tutorial showed how to create a simple console-based DICOM application that finds elements in a dataset using two different pairs of methods from the DicomDataSet class.

See Also

Help Version 21.0.2023.3.1
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.