Aspose.Medical  for .NET

Anonymize DICOM Files in .NET C#

Remove patient identifying information from DICOM files using DICOM PS 3.15 confidentiality profiles. Ensure HIPAA and GDPR compliance with a pure .NET library.

  Download Free Trial

Standards-Based DICOM Anonymization

Aspose.Medical for .NET provides a comprehensive DICOM anonymization API based on the DICOM PS 3.15 Confidentiality Profiles. The API enables healthcare developers to remove or modify patient identifying information (PII) from DICOM files while preserving the clinical value of the imaging data. Unlike simple tag-removal approaches, Aspose.Medical follows the official DICOM standard to ensure proper de-identification.

The Anonymizer class works with ConfidentialityProfile objects that define exactly how each DICOM attribute should be handled — whether it should be removed, replaced with a dummy value, cleaned, or kept unchanged. This approach ensures consistent, auditable anonymization that meets regulatory requirements.

Anonymize a DICOM File in C#

The simplest way to anonymize a DICOM file is to use the default confidentiality profile. This applies the DICOM PS 3.15 Basic Profile, which handles all standard patient-identifying attributes:

Basic DICOM anonymization - C#

// Load a DICOM file
DicomFile dicomFile = DicomFile.Open("input.dcm");

// Create an anonymizer with the default profile
Anonymizer anonymizer = new();

// Anonymize the DICOM file (non-destructive - returns a new file)
DicomFile anonymizedFile = anonymizer.Anonymize(dicomFile);

// Save the anonymized file
anonymizedFile.Save("anonymized_output.dcm");

The Anonymize method is non-destructive — it clones the original dataset and returns a new anonymized copy. The original DICOM file remains unchanged, which is essential for audit trails and data integrity workflows.

Custom Patient Identity Replacement

In many workflows, you need to replace patient information with specific alias values rather than simply removing it. The ConfidentialityProfile class allows you to set replacement values for the patient name and patient ID:

Anonymize with custom patient identity - C#

// Load a DICOM file
DicomFile dicomFile = DicomFile.Open("input.dcm");

// Create a confidentiality profile with custom replacement values
ConfidentialityProfile profile = new()
{
    PatientName = "ANONYMOUS PATIENT",
    PatientId = "00000000"
};

// Create an anonymizer with this profile
Anonymizer anonymizer = new(profile);

// Anonymize the file
DicomFile anonymizedFile = anonymizer.Anonymize(dicomFile);

// Save the anonymized file
anonymizedFile.Save("custom_anonymized.dcm");

In-Place Anonymization

For scenarios where memory efficiency is important or you don't need to keep the original data, the API provides in-place anonymization that modifies the dataset directly:

In-place DICOM anonymization - C#

// Load a DICOM file
DicomFile dicomFile = DicomFile.Open("input.dcm");

// Create anonymizer
Anonymizer anonymizer = new();

// Perform in-place anonymization (modifies the original dataset)
anonymizer.AnonymizeInPlace(dicomFile);

// Save the modified file
dicomFile.Save("inplace_anonymized.dcm");

Both Anonymize and AnonymizeInPlace methods also accept a Dataset object directly, giving you full control over which dataset is processed.

Configurable Confidentiality Profile Options

The DICOM PS 3.15 standard defines a Basic Profile along with several optional profiles that control how specific categories of data are handled during anonymization. You can combine these options using the ConfidentialityProfileOptions enum:

OptionDescription
BasicProfileThe default DICOM PS 3.15 Basic Application Level Confidentiality Profile
RetainSafePrivateRetain safe private attributes during anonymization
RetainUIDsKeep original DICOM UIDs (Study, Series, Instance) unchanged
RetainDeviceIdentRetain device identification attributes (manufacturer, model, station name)
RetainInstitutionIdentRetain institution identification attributes (name, address, department)
RetainPatientCharsRetain patient characteristics (age, sex, size, weight) for research purposes
RetainLongFullDatesRetain full date and time values for longitudinal studies
RetainLongModifDatesRetain modified dates for longitudinal studies
CleanDescClean text descriptions that may contain identifying information
CleanStructdContClean structured content that may contain identifying information
CleanGraphClean graphical data that may contain burned-in patient information

Anonymize with specific profile options - C#

// Create a profile that retains patient characteristics and UIDs for research
var options = ConfidentialityProfileOptions.BasicProfile
    | ConfidentialityProfileOptions.RetainPatientChars
    | ConfidentialityProfileOptions.RetainUIDs;

var profile = ConfidentialityProfile.CreateDefault(options);
var anonymizer = new Anonymizer(profile);

Action Codes for Attribute Handling

Each attribute in a confidentiality profile is assigned an action code that defines how it should be processed during anonymization. These follow the DICOM PS 3.15 Table E.1-1a standard:

CodeActionDescription
DReplace with dummyReplace with a non-zero length value consistent with the VR
ZZero-length or dummyReplace with a zero-length value or a dummy value consistent with the VR
XRemoveRemove the attribute completely from the dataset
KKeepKeep the attribute unchanged (for non-sequences) or clean (for sequences)
CCleanReplace with a cleaned value of similar meaning consistent with the VR
UReplace UIDReplace with a new UID that is internally consistent within the set of instances

Custom Profiles from CSV, JSON, and XML

For advanced use cases, you can define custom anonymization profiles by loading rules from external CSV, JSON, or XML files. This allows you to tailor the anonymization behavior to your specific organizational policies or regulatory requirements:

Load anonymization profile from CSV - C#

// CSV format: TagPattern;Action
// 0010,0010;Z   (Anonymize PatientName)
// 0010,0020;D   (Replace PatientID with dummy)
// 0020,000D;U   (Replace StudyInstanceUID)

var profile = ConfidentialityProfile.LoadFromCsvFile(
    "anonymization_rules.csv",
    ConfidentialityProfileOptions.BasicProfile);

var anonymizer = new Anonymizer(profile);

Load anonymization profile from JSON - C#

// JSON format:
// [
//     { "Tag": "0010,0010", "Action": "Z" },
//     { "Tag": "0010,0020", "Action": "D" },
//     { "Tag": "0020,000D", "Action": "U" }
// ]

var profile = ConfidentialityProfile.LoadFromJsonFile(
    "anonymization_rules.json",
    ConfidentialityProfileOptions.BasicProfile);

var anonymizer = new Anonymizer(profile);

Custom profiles can also be loaded from XML files (LoadFromXmlFile), streams (LoadFromJsonStream, LoadFromXmlStream), or directly from strings (LoadFromCsvString, LoadFromJsonString, LoadFromXmlString).

HIPAA and GDPR Compliance

DICOM files routinely contain Protected Health Information (PHI) as defined by HIPAA and personal data as defined by GDPR. Aspose.Medical for .NET helps you meet regulatory requirements by providing:

  • DICOM PS 3.15 compliance: The anonymization engine follows the official DICOM standard for de-identification, ensuring recognized best practices are applied to all relevant attributes.
  • Comprehensive PII removal: Patient name, ID, date of birth, address, and other identifiers are handled according to the configured confidentiality profile.
  • UID replacement: Study, Series, and SOP Instance UIDs can be replaced with new, internally consistent UIDs to prevent re-identification through cross-referencing.
  • Configurable retention: When research or clinical needs require it, specific categories of data (patient characteristics, dates, device information) can be selectively retained using the standard option profiles.
  • Auditable process: The standards-based approach with defined action codes provides a clear, documentable anonymization process for regulatory audits.
  

Support and Learning Resources