Learn how to Read and View a DICOM File in C#
Created
July 9, 2025 11:44
-
-
Save aspose-com-gists/a7638d4d2449cec91ff17f448b1e662e to your computer and use it in GitHub Desktop.
Read and View a DICOM File in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the saved DICOM file | |
Aspose.Medical.Dicom.DicomFile dicomFile = DicomFile.Open("sample.dcm"); | |
Aspose.Medical.Dicom.Dataset dataset = dicomFile.Dataset; | |
// Read patient-related information | |
string patientID = dataset.GetSingleValue<string>(Tag.PatientID); | |
string patientName = dataset.GetSingleValue<string>(Tag.PatientName); | |
string birthDateStr = dataset.GetSingleValue<string>(Tag.PatientBirthDate); | |
DateTime birthDate = DateTime.ParseExact(birthDateStr, "yyyyMMdd", null); | |
// Read study information | |
string studyDateStr = dataset.GetSingleValue<string>(Tag.StudyDate); | |
DateTime studyDate = DateTime.ParseExact(studyDateStr, "yyyyMMdd", null); | |
string studyID = dataset.GetSingleValue<string>(Tag.StudyID); | |
// Read image metadata | |
int rows = dataset.GetSingleValue<int>(Tag.Rows); | |
int columns = dataset.GetSingleValue<int>(Tag.Columns); | |
int bitsAllocated = dataset.GetSingleValue<int>(Tag.BitsAllocated); | |
string modality = dataset.GetSingleValue<string>(Tag.Modality); | |
byte[] pixelData = dataset.GetValues<byte>(Tag.PixelData).ToArray(); // Use ToArray() for display | |
// Display information | |
Console.WriteLine($"Patient ID: {patientID}"); | |
Console.WriteLine($"Patient Name: {patientName}"); | |
Console.WriteLine($"Birth Date: {birthDate:yyyy-MM-dd}"); | |
Console.WriteLine($"Study Date: {studyDate:yyyy-MM-dd}"); | |
Console.WriteLine($"Study ID: {studyID}"); | |
Console.WriteLine($"Image Rows: {rows}"); | |
Console.WriteLine($"Image Columns: {columns}"); | |
Console.WriteLine($"Bits Allocated: {bitsAllocated}"); | |
Console.WriteLine($"Modality: {modality}"); | |
Console.WriteLine($"Pixel Data Length: {pixelData.Length} bytes"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment