Delving into the world of medical imaging and data exchange, DICOM (Digital Imaging and Communications in Medicine) plays a pivotal role in healthcare. For C# developers aiming to work with DICOM, understanding how to read, write, and manipulate DICOM files is essential. This article explores C# DICOM examples, guiding you through the process with clear, concise code snippets.

Before we dive into the C# DICOM examples, let's briefly understand the DICOM format. DICOM is a standard for handling, storing, printing, and transmitting information in medical imaging. It supports various data types, including images, documents, and signals. At its core, DICOM files are a collection of Data Elements, organized into Groups and representing metadata and pixel data.

Working with DICOM Files in C#
To start working with DICOM files in C#, you'll first need to install the DCM4CHEE library, a popular choice for managing DICOM data in .NET. You can install it via NuGet package manager in Visual Studio.

Once installed, you can begin reading and writing DICOM files. Let's dive into some examples.
Reading DICOM Files

Reading DICOM files involves opening the file and parsing its contents. Here's a simple example of how to read a DICOM file using the DcmObject class from DCM4CHEE:
using (var fs = new FileStream("path\\to\\your\\file.dcm", FileMode.Open, FileAccess.Read))
{
var dicomFile = new DcmObject(fs);
Console.WriteLine("Pixel Data Present: " + dicomFile.Dataset.PixelData Present);
}
The above code reads a DICOM file and checks if it contains pixel data.
Writing DICOM Files

Writing DICOM files involves creating a new DcmObject, setting its attributes, and writing it to a file. Here's an example of creating a new DICOM file with some basic metadata:
var dicomFile = new DcmObject();
dicomFile.FileMetaInfo.TransferSyntaxUID = DcmElement.KeyValuePair.TransferSyntaxExplicitVRLittleEndian;
dicomFile.Dataset.AddOrUpdate(DcmElement.SpecificCharacterSet, "ISO_IR 100");
dicomFile.Dataset.AddOrUpdate(DcmElement.ImageType, "ORIGINAL", "PRIMARY");
dicomFile.Dataset.AddOrUpdate(DcmElement.PatientName, "DOE^JOHN");
dicomFile.Dataset.AddOrUpdate(DcmElement.PatientID, "1234567890");
dicomFile.Save("path\\to\\your\\output.dcm");
In this example, we're creating a new DICOM file with specific character set, image type, patient name, and patient ID attributes set. The file is then saved to the specified path.
Manipulating DICOM Files

DCM4CHEE provides additional functionality for manipulating DICOM files. For instance, you can extract specific data elements, update existing attributes, or add new ones.
Extracting Data Elements









To extract specific data elements from a DICOM file, use the Get method on the DcmDataset object. Here's an example of extracting the patient name:
var patientName = dicomFile.Dataset.Get(DcmElement.PatientName).GetString(0);
Console.WriteLine("Patient Name: " + patientName);
Updating Data Elements
You can update existing data elements using the AddOrUpdate method on the DcmDataset object. Here's how you can change the patient name:
dicomFile.Dataset.AddOrUpdate(DcmElement.PatientName, "NEW PATIENT NAME");
dicomFile.Save("path\\to\\your\\updated.dcm");
C# DICOM manipulation can go beyond simple reads and writes, delving into complex operations like adding images, updating metadata, and more. With DCM4CHEE, these tasks become significantly easier.
As we've explored, working with DICOM files in C# involves a combination of reading, writing, and manipulating data. The examples provided here should give you a solid foundation to build upon. Whether you're reading an image for display, updating metadata, or writing new files, DCM4CHEE's power and simplicity make it an excellent choice for C# DICOM work.