Initialize a DICOM DataSet and Set the PixelData Tag - WinForms C#

This tutorial shows how to create a DicomDataSet object, initialize the dataset, and set a raster image in the PixelData tag.

Summary This tutorial covers how to initialize a DICOM DataSet and set an image in the PixelData tag 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

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 Initialize a DICOM DataSet and Set the PixelData Tag - 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:


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

Add the Form Controls

In Solution Explorer, double-click Form1.cs to display it in the Designer. Go to the Toolbox and add the following controls:

Note that the image above shows the fields filled with placeholder values taken from the DICOM sample dataset image3.dcm found in <INSTALL_DIR>\Resources\Images\DICOM.

Initialize the DicomDataSet

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.

// Using block at the top  
using System; 
using System.IO; 
using System.Windows.Forms; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Dicom; 
// Add this global variable  
private DicomDataSet dataSet; 

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.

private void Form1_Load(object sender, EventArgs e)  

Double-click on the initializeDataSetButton button control to add a click event handler. Use the following code to create a new DicomDataSet instance and initialize it with the values from the text box controls.

private void initializeDataSetButton_Click(object sender, EventArgs e) 
      dataSet = new DicomDataSet(); 
      // Initialize the DicomDataSet as secondary capture 
      dataSet.Initialize(DicomClassType.SCImageStorage, DicomDataSetInitializeFlags.AddMandatoryElementsOnly | DicomDataSetInitializeFlags.ExplicitVR); 
      // Add patient name 
      dataSet.InsertElementAndSetValue(DicomTag.PatientName, patientNameTextBox.Text); 
      // Set the UIDs for the DICOM File 
      dataSet.InsertElementAndSetValue(DicomTag.SeriesInstanceUID, seriesInstanceUidTextBox.Text); 
      dataSet.InsertElementAndSetValue(DicomTag.StudyInstanceUID, studyInstanceUidTextBox.Text); 
      dataSet.InsertElementAndSetValue(DicomTag.SOPInstanceUID, sopInstanceUidLabel.Text); 
      // Set the Image Type to Derived, since this is not created from a primary capture device 
      dataSet.InsertElementAndSetValue(DicomTag.ImageType, "DERIVED"); 
      MessageBox.Show("DataSet successfully initialized with the entered values"); 
   catch (Exception ex) 

Note that the class type of the generated DICOM dataset is an SCImageStorage which is a Secondary Capture Image Storage dataset. For more details on other available class types, follow the link in the See Also section below.

Load and Set a Raster Image in the PixelData Tag

Double-click on the setPixelDataButton button control to add a click event handler. Use the following code to load a raster image, set it in the dataset, and save the output.

private void setPixelDataButton_Click(object sender, EventArgs e) 
   RasterImage image; 
      // Load Raster Image 
      using (RasterCodecs codecs = new RasterCodecs()) 
         OpenFileDialog loadDlg = new OpenFileDialog(); 
         loadDlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images"; 
         if (loadDlg.ShowDialog(this) == DialogResult.OK) 
            image = codecs.Load(loadDlg.FileName); 
      // Set Image in PixelData Tag 
      if (image != null && dataSet != null) 
         DicomElement pixelData = dataSet.InsertElement(null, false, DicomTag.PixelData, DicomVRType.UN, false, 0); 
         dataSet.SetImage(pixelData, image, DicomImageCompressionType.None, DicomImagePhotometricInterpretationType.Monochrome2, 16, 2, DicomSetImageFlags.AutoSetVoiLut); 
      MessageBox.Show("Image successfully set. Select location to save dataset."); 
      // Save DataSet 
      SaveFileDialog saveDlg = new SaveFileDialog(); 
      saveDlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images\DICOM"; 
      saveDlg.Filter = "DICOM DataSets (*.dcm)|*.dcm"; 
      saveDlg.FileName = "InitializedDS.dcm"; 
      if (saveDlg.ShowDialog(this) == DialogResult.OK) 
         dataSet.Save(saveDlg.FileName, DicomDataSetSaveFlags.ExplicitVR); 
         MessageBox.Show("Dataset successfully saved."); 
   catch (Exception ex) 

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 the user to create and initialize a DicomDataSet with several tag values. The user can also load a raster image to be used to set the PixelData tag in the dataset before saving the result to file.


This tutorial showed how to add the necessary references to create and initialize a DicomDataSet object along with how to set a raster image in the PixelData tag.

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.