Mastering C# FO/DICOM: Practical Examples

By Teresa

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.

Facebook
Facebook

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.

Uncover truths about D&C.
Uncover truths about D&C.

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.

the cerebellum and its functions
the cerebellum and its functions

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

Reading DICOM Files

EF Core Persistence Layer with DDD in .NET: Repositories, DbContext, Specifications, and CRM Example
EF Core Persistence Layer with DDD in .NET: Repositories, DbContext, Specifications, and CRM Example

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

Foetal cortical expansion is associated with neurodevelopmental outcome at 2-years in congenital heart disease: a longitudinal follow-up study
Foetal cortical expansion is associated with neurodevelopmental outcome at 2-years in congenital heart disease: a longitudinal follow-up study

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

Cranial Nerves Notes, Neurosurgeon Notes, Ent Notes, Medical Shorthand, Development Of Teeth Histology, Dental Nurse Revision, Medical Shorthand List, Neurology Language Study Guide, Blade Teeth Anatomy Diagram
Cranial Nerves Notes, Neurosurgeon Notes, Ent Notes, Medical Shorthand, Development Of Teeth Histology, Dental Nurse Revision, Medical Shorthand List, Neurology Language Study Guide, Blade Teeth Anatomy Diagram

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

Correlation vs Causation — What's the Difference?
Correlation vs Causation — What's the Difference?
Critical roles of Gi/o proteins and phospholipase C-δ1 in the activation of receptor-operated TRPC4 channels | PNAS
Critical roles of Gi/o proteins and phospholipase C-δ1 in the activation of receptor-operated TRPC4 channels | PNAS
Multi-modal AI for comprehensive breast cancer prognostication - Nature Communications
Multi-modal AI for comprehensive breast cancer prognostication - Nature Communications
Contrastive language image pretraining for a cardiac magnetic resonance image embedding with zero-shot capabilities - Nature Communications
Contrastive language image pretraining for a cardiac magnetic resonance image embedding with zero-shot capabilities - Nature Communications
Encoding model of temporal processing in human visual cortex | PNAS
Encoding model of temporal processing in human visual cortex | PNAS
Instagram D1 Vs D2 Flexion, Physical Therapy School, Human Muscle Anatomy, Muscle Anatomy, Nursing Tips, Muscle Function, Athletic Training, Body Anatomy, Occupational Therapy
Instagram D1 Vs D2 Flexion, Physical Therapy School, Human Muscle Anatomy, Muscle Anatomy, Nursing Tips, Muscle Function, Athletic Training, Body Anatomy, Occupational Therapy
Enhanced CD4+ and CD8+ T cell infiltrate within convex hull defined pancreatic islet borders as autoimmune diabetes progresses - Scientific Reports
Enhanced CD4+ and CD8+ T cell infiltrate within convex hull defined pancreatic islet borders as autoimmune diabetes progresses - Scientific Reports
A scalable and cGMP-compatible autologous organotypic cell therapy for Dystrophic Epidermolysis Bullosa - Nature Communications
A scalable and cGMP-compatible autologous organotypic cell therapy for Dystrophic Epidermolysis Bullosa - Nature Communications
Activation of CGRP receptor–mediated signaling promotes tendon-bone healing
Activation of CGRP receptor–mediated signaling promotes tendon-bone healing

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.