
In our last post, we showed you how to create and save a DICOM file in C# using Aspose.Medical for .NET. The next step is to read the saved DICOM file programmatically in C#. In this blog post, you will learn how to load, open, and read DICOM files using C#. You can use this guide as a starting point to create your own DCM file viewer application.
This article covers the following topics:
Aspose.Medical—DCM File Viewer
For loading, reading, and viewing DICOM files, we will use Aspose.Medical for .NET. It provides a developer-friendly API to read, analyze, and extract DICOM data effortlessly. The Dataset
object inside a DicomFile
class of the API makes it easy to work with medical metadata.
Before we begin, ensure the following:
- Download the SDK from the releases or install the package via NuGet Package Manager using the following command:
Install-Package Aspose.Medical
- You already have a DICOM file saved (e.g., sample.dcm) from the previous tutorial.
How to Read a DICOM File: Step-by-Step Guide
Load the DICOM File
You can load the .dcm
file into a DicomFile
object and access its dataset, as demonstrated below:
// Load an existing DICOM file
DicomFile dicomFile = new DicomFile("sample.dcm");
Aspose.Medical.Dicom.Dataset dataset = dicomFile.Dataset;
Reading Values from a DICOM File
Once the file is loaded, you can access key metadata using DICOM tags:
string patientName = dataset.GetSingleValue<string>(Tag.PatientName);
Console.WriteLine($"Patient Name: {patientName}");
You can read hundreds of DICOM tags this way depending on what information you need.
Access Pixel Data
If you want to retrieve image data from the DICOM file, use:
byte[] pixelData = dataset.GetValues<byte>(Tag.PixelData).ToArray(); // Use ToArray() for display
Console.WriteLine($"Pixel Data Length: {pixelData.Length} bytes");
You can use this byte array to render the image or feed it into further processing algorithms.
Retrieving a Default Value if a Tag is Missing
When tags are optional, you might want to provide fallback values. Use GetSingleValueOrDefault to retrieve data or return a default if the tag is absent.
string institutionName = dataset.GetSingleValueOrDefault<string>(Tag.InstitutionName, "Unknown");
Console.WriteLine("Institution Name: " + institutionName);
This is ideal for scenarios where tag presence is unpredictable.
Retrieving a Specific Value from a Multi-Valued Tag
If a tag contains multiple values, but you only need one, use GetValue with an index or range expression.
double imagePositionX = dataset.GetValue<double>(Tag.ImagePositionPatient, ^1);
Console.WriteLine("First Image Position X: " + imagePositionX);
Note: This method will throw an exception if the tag is missing.
DCM File Viewer: Complete Code
Here is a complete working code that shows how to load a DICOM file and read the available tag’s information:
Output
Patient ID: JD123456
Patient Name: John Doe
Birth Date: 1985-07-20
Study Date: 2025-07-07
Study ID: Study001
Image Rows: 256
Image Columns: 256
Bits Allocated: 8
Modality: OT
Pixel Data Length: 65536 bytes
Try it Yourself
You can use any .dcm
file, whether generated using Aspose or received from an imaging device. Simply load the file and read the tags, and you are all set.
Get a Free License
You can get a free temporary license to explore the full functionality of Aspose.Medical for .NET without any restrictions. It’s quick, easy, and takes just a minute.
DICOM File Viewer: Free Resources
You can enhance your development experience with these valuable resources:
- Developer Guide – Step-by-step tutorials and usage examples.
- API Reference – Complete class and method documentation.
- Aspose.Medical Blog – Latest updates, tips, and feature highlights.
Conclusion
In this article, we have seen how to read and extract data from a DICOM file in C#. As a developer, you can use Aspose.Medical for .NET to work with DICOM (.dcm) files and build your own DCM file viewer application.
If you have questions or run into issues while working with DICOM files, don’t hesitate to ask on the Aspose Forums. The support team is always ready to help.