Generate and Set SOPInstanceUID in a DICOM DataSet - Console C#

This tutorial shows how to generate and insert a Unique Identifier (UID) for the Service Object Pair (SOP) instance in a generated DICOM DataSet.

Overview  
Summary This tutorial covers how to generate and insert an SOPInstance UID in a C# Windows Console Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 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 Load and Display a DICOM Image in the Medical Viewer - WinForms C# tutorial.

DICOM Unique Identifiers

Unique Identifiers (UIDs) provide the capability to uniquely identify a wide variety of items. They guarantee uniqueness across multiple countries, sites, vendors and equipment. Different classes of objects, instance of objects and information entities can be distinguished from one another across the DICOM universe of discourse irrespective of any semantic context.

For example, the same UID value cannot be used to identify both a study instance (Study Instance UID) and a series instance (Series Instance UID) within that study or a different study. Implementers also need to be cautioned against building new UID values by derivation (for example by adding a suffix) from a UID assigned by another implementation.

Each UID is composed of two parts, an <org root> and a <suffix>: UID = <org root>.<suffix>

This tutorial will show how the <suffix> can be generated. The approach here derives the unique identifier using the Date and Time.

Note that the <org root> "1.2.840.10008" is reserved for DICOM defined items (such as DICOM Transfer Syntaxes) and shall not be used for privately defined items (such as an Image Instance).

The tutorial uses "1.2.840.10008" as a placeholder which should be replaced with a valid organization identifier.

For the specific rules on the DICOM Unique Identifiers, see the link included in the See Also section below.

Create the Project and Add the LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the following 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 tutorial.

Add the Generate UID Code

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

In the Solution Explorer, open Program.cs and add the following to the using block and also set the global variable orgroot.

C#
// Using block at the top  
using System; 
using System.IO; 
using System.Linq; 
using Leadtools; 
using Leadtools.Dicom; 
C#
// Add this global variable  
static string orgroot = "1.2.840.10008"; // Change this to your organization’s root value 

Call the two methods, GenerateNewUID(string orgroot) and SetSOPInstancedUID(string uid), inside the Main method after setting the license.

C#
static void Main(string[] args) 
{ 
   try 
   { 
      SetLicense(); 
 
      //Generate a new unique Identifier 
      string uid = GenerateNewUID(orgroot); 
 
      //Set the Unique Identifier into a Dicom DataSet 
      SetSOPInstancedUID(uid); 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.Message); 
   } 
} 

Use the code below to generate the new UID.

C#
private static string GenerateNewUID(string orgroot) 
{ 
   Console.WriteLine($"<org root> portion of the UID: {orgroot}"); 
 
   // Gets date/time 
   var now = DateTime.Now; 
 
   // Gets numeric date without the year and the exact military time 
   string zeroDate = now.Month.ToString() + now.Day.ToString() + "." + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + now.Millisecond.ToString(); 
 
   // Generate a globally unique identifier 
   string guid = new string(Guid.NewGuid().ToString().Where(char.IsDigit).ToArray()); 
 
   Console.WriteLine($"<suffix> portion of the UID: {zeroDate}.{guid}"); 
 
   // Combines the strings 
   string sopUid = $"{orgroot}.{zeroDate}.{guid}"; 
 
   // Check the length to see if it is over 64 characters, if it is, then use a substring 
   sopUid = sopUid.Length > 64 ? sopUid.Substring(0, 64) : sopUid; 
 
   Console.WriteLine($"SOPInstanceUID: {sopUid}"); 
 
   return sopUid; 
} 

Add the Set SOPInstance UID Code

Use the code below to set the generated UID as the SOPInstanceUID in a DICOM dataset.

C#
private static void SetSOPInstancedUID(string uid) 
{ 
   // Start Dicom engine 
   DicomEngine.Startup(); 
 
   DicomDataSet ds = new DicomDataSet(); 
 
   // Removes all the items from the Data Set 
   ds.Reset(); 
 
   // Creates the Data Set object, inserting the appropriate elements for the specified class. 
   ds.Initialize(DicomClassType.SCImageStorage, DicomDataSetInitializeFlags.AddMandatoryElementsOnly | DicomDataSetInitializeFlags.ExplicitVR); 
 
   // Finds or inserts (if not found) an element in the data set and sets the value of the element 
   ds.InsertElementAndSetValue(DicomTag.SOPInstanceUID, uid); 
 
   Console.WriteLine($"Generated SOPInstanceUID embedded in DICOM DataSet successfully"); 
} 

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 generates the UID using the supplied <orgroot> and Date and Time. The generated UID is then inserted in a DICOM DataSet.

Wrap-up

This tutorial showed how to generate a DICOM UID and insert it in the SOPInstanceUID tag for a DICOM dataset using the InsertElement() method.

See Also

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


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