You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助:基于Fo-Dicom实现DICOM头增删改查及像素数据存储(C#桌面端)

Hey there! Let's tackle your Fo-Dicom needs step by step—whether you're modifying DICOM headers or embedding pixel data, I've got practical code snippets and explanations to help you out in your C# desktop app.

Working with DICOM Headers & Pixel Data in Fo-Dicom (C# Desktop)

Prerequisites

First, make sure you've added Fo-Dicom to your project via NuGet. You can do this using the Package Manager Console:

Install-Package fo-dicom

Or via .NET CLI:

dotnet add package fo-dicom

1. DICOM Header Operations (Add/Update/Delete/Query)

Fo-Dicom uses the DicomDataset class as the core container for all DICOM tag data. Here's how to handle each operation:

Add/Update DICOM Tags

Adding and updating tags use similar logic—Fo-Dicom's AddOrUpdate method handles both cases (creates the tag if it doesn't exist, overwrites it if it does). For custom private tags, you'll need to define the tag explicitly first:

// Load an existing DICOM file, or create a new empty one
var dicomFile = DicomFile.Open("path/to/your/dicom.dcm");
var dataset = dicomFile.Dataset;

// Add/update a standard tag (Patient Name)
dataset.AddOrUpdate(DicomTag.PatientName, "Smith^Alice");

// Add a custom private tag
var privateTag = new DicomTag(0x7000, 0x0010, DicomVR.LO, "MyAppPrivateCreator");
dataset.AddOrUpdate(privateTag, "CustomPatientNote");

// Save the modified file
dicomFile.Save("path/to/updated_dicom.dcm");

Delete DICOM Tags

Always check if a tag exists before removing it to avoid exceptions, or use the safe TryRemove method:

// Remove a single tag safely
if (dataset.TryRemove(DicomTag.PatientAddress, out _))
{
    Console.WriteLine("Patient Address tag removed successfully");
}

// Remove multiple tags at once
var tagsToCleanup = new[] { DicomTag.PatientPhone, DicomTag.PatientEmail };
foreach (var tag in tagsToCleanup)
{
    dataset.TryRemove(tag, out _);
}

// Save changes
dicomFile.Save("path/to/cleaned_dicom.dcm");

Query DICOM Tags

Use type-specific TryGet methods to safely retrieve tag values, or iterate through all tags to inspect the full dataset:

// Get a string tag value (Patient Name)
if (dataset.TryGetString(DicomTag.PatientName, out var patientName))
{
    Console.WriteLine($"Patient Name: {patientName}");
}

// Get a numeric tag value (Study ID)
if (dataset.TryGetInt32(DicomTag.StudyID, out var studyId))
{
    Console.WriteLine($"Study ID: {studyId}");
}

// Iterate through all tags in the dataset
foreach (var element in dataset.Elements)
{
    Console.WriteLine($"Tag: {element.Tag}, VR: {element.ValueRepresentation}, Value: {element.ToString()}");
}

2. Embedding Image Pixel Data into DICOM File

To add pixel data, you'll need to set mandatory DICOM metadata (like image dimensions, bit depth, and UIDs) before converting your image data into a DicomPixelData object.

From a Bitmap Image

If you're working with standard image files (PNG, JPG), you can directly convert a Bitmap to DICOM pixel data:

// Load your source image
using var sourceImage = new Bitmap("path/to/your/image.png");

// Create a new DICOM file with required metadata
var dicomFile = new DicomFile();
var dataset = dicomFile.Dataset;

// Set mandatory DICOM tags for image storage
dataset.AddOrUpdate(DicomTag.PatientName, "Smith^Alice");
dataset.AddOrUpdate(DicomTag.StudyInstanceUID, DicomUID.Generate());
dataset.AddOrUpdate(DicomTag.SeriesInstanceUID, DicomUID.Generate());
dataset.AddOrUpdate(DicomTag.SOPInstanceUID, DicomUID.Generate());
dataset.AddOrUpdate(DicomTag.SOPClassUID, DicomUID.CTImageStorage); // Adjust based on your image type
dataset.AddOrUpdate(DicomTag.Rows, sourceImage.Height);
dataset.AddOrUpdate(DicomTag.Columns, sourceImage.Width);
dataset.AddOrUpdate(DicomTag.BitsAllocated, 8);
dataset.AddOrUpdate(DicomTag.BitsStored, 8);
dataset.AddOrUpdate(DicomTag.HighBit, 7);
dataset.AddOrUpdate(DicomTag.PixelRepresentation, 0); // 0 = unsigned, 1 = signed
// Use "MONOCHROME2" for grayscale images instead
dataset.AddOrUpdate(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);

// Convert bitmap to DICOM pixel data and add the frame
using var pixelData = DicomPixelData.Create(dataset, true);
pixelData.AddFrame(sourceImage);

// Save the final DICOM file
dicomFile.Save("path/to/image_with_pixels.dcm");

From a Raw Pixel Array

If you have a raw byte array of pixel data (e.g., from a sensor), you can directly inject it into the DICOM dataset:

// Example: 512x512 8-bit grayscale pixel array
int width = 512;
int height = 512;
byte[] rawPixels = new byte[width * height]; // Populate this array with your pixel data

// Create dataset with mandatory metadata
var dataset = new DicomDataset();
dataset.AddOrUpdate(DicomTag.PatientName, "Smith^Alice");
dataset.AddOrUpdate(DicomTag.StudyInstanceUID, DicomUID.Generate());
dataset.AddOrUpdate(DicomTag.SeriesInstanceUID, DicomUID.Generate());
dataset.AddOrUpdate(DicomTag.SOPInstanceUID, DicomUID.Generate());
dataset.AddOrUpdate(DicomTag.SOPClassUID, DicomUID.CTImageStorage);
dataset.AddOrUpdate(DicomTag.Rows, height);
dataset.AddOrUpdate(DicomTag.Columns, width);
dataset.AddOrUpdate(DicomTag.BitsAllocated, 8);
dataset.AddOrUpdate(DicomTag.BitsStored, 8);
dataset.AddOrUpdate(DicomTag.HighBit, 7);
dataset.AddOrUpdate(DicomTag.PixelRepresentation, 0);
dataset.AddOrUpdate(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Monochrome2.Value);

// Inject raw pixel data
using var pixelData = DicomPixelData.Create(dataset, true);
pixelData.AddFrame(rawPixels);

// Save to file
var dicomFile = new DicomFile(dataset);
dicomFile.Save("path/to/raw_pixel_dicom.dcm");

Quick Tips

  • Always validate your modified DICOM files with a DICOM viewer to ensure compliance with DICOM standards.
  • For private tags, make sure to register the private creator tag first if your use case requires it.
  • When working with large pixel datasets, consider using streaming to avoid excessive memory usage.

内容的提问来源于stack exchange,提问作者Ahsan Iqbal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:34:14