This tutorial shows how to create a DicomDataSet
object, initialize the dataset, and set a raster image in the PixelData tag in a WinForms C# application.
Overview | |
---|---|
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 (6 KB) |
Platform | .NET 6 WinForms C# Application |
IDE | Visual Studio 2022 |
Development License | Download LEADTOOLS |
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.
Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have the 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). For this project, the following references are needed:
If NuGet references are used, this tutorial requires the following NuGet package:
Leadtools.Dicom.Pacs.Scu
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Core.dll
Leadtools.Dicom.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:
Note: Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.
In the Solution Explorer, double-click Form1.cs
to display it in the Designer. Go to the Toolbox and add the following controls:
initializeDataSetButton
with the text: Initialize DataSetsetPixelDataButton
with the text: Set PixelDatapatientNameTextBox
seriesInstanceUidTextBox
studyInstanceUidTextBox
sopInstanceUidTextBox
Four Label controls to identify each TextBox.
Note that the image above shows the fields filled with placeholder values taken from the DICOM sample dataset image3.dcm
, found in <INSTALL_DIR>\LEADTOOLS23\Resources\Images\DICOM
.
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 Leadtools;
using Leadtools.Codecs;
using Leadtools.Dicom;
// Add this global variable
private DicomDataSet dataSet;
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)
{
DicomEngine.Startup();
}
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)
{
try
{
dataSet = new DicomDataSet();
dataSet.Reset();
// 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)
{
MessageBox.Show(ex.Message);
}
}
Note that the class type of the generated DICOM dataset is 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.
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;
try
{
// Load Raster Image
using (RasterCodecs codecs = new RasterCodecs())
{
OpenFileDialog loadDlg = new OpenFileDialog();
loadDlg.InitialDirectory = @"C:\LEADTOOLS23\Resources\Images";
if (loadDlg.ShowDialog(this) == DialogResult.OK)
{
image = codecs.Load(loadDlg.FileName);
}
else
return;
}
// 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);
image.Dispose();
}
MessageBox.Show("Image successfully set. Select location to save dataset.");
// Save DataSet
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.InitialDirectory = @"C:\LEADTOOLS23\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)
{
MessageBox.Show(ex.Message);
}
}
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.