Create DICOM file

Create DICOM file

Aspose.Medical provides the Aspose.Medical.Dicom.DicomFile class, which allows users to create, modify, and work with DICOM files. To create Aspose.Medical.Dicom.DicomFile from scratch, you can simply initialize a new instance of the Aspose.Medical.Dicom.DicomFile class using any available constructor.

Create DICOM file from scratch

For scenarios where an empty DICOM file needs to be initialized before adding data, use the parameterless constructor.

// Create an empty DICOM file
Aspose.Medical.Dicom.DicomFile dicomFile = new();

// Add data to the newly created DICOM file
dicomFile.Dataset.AddOrUpdate(Aspose.Medical.Dicom.Tags.Tag.XAAcquisitionFrameRate, 17.95);

Create DICOM file in .NET based on existing data

The constructor taking another DicomFile copies its dataset and file meta information. Constructors taking a Dataset or a MetaInformation and Dataset retain those supplied objects. Changes through the new file therefore also affect those objects. Use the Dataset and MetaInformation copy constructors first when you need independent values.

Creating a DICOM File using an Existing DICOM File

If you have an existing DICOM file, you can create a new Aspose.Medical.Dicom.DicomFile instance by copying data from another Aspose.Medical.Dicom.DicomFile object.

// Load an existing DICOM file
Aspose.Medical.Dicom.DicomFile existingFile = DicomFile.Open("input.dcm");

// Create a copy of the DICOM file
Aspose.Medical.Dicom.DicomFile dicomFile = new(existingFile);

// Add data to the newly created DICOM file
dicomFile.Dataset.AddOrUpdate(Aspose.Medical.Dicom.Tags.Tag.XAAcquisitionFrameRate, 17.95);

Creating a DICOM File from a Dataset

The DicomFile(Dataset) constructor builds file meta information from the supplied dataset, which must already contain SOP Class UID and SOP Instance UID. Supply the appropriate SOP Class and a unique instance UID assigned by your application. The following values illustrate initialization; creating an empty or partially populated dataset does not make a complete DICOM information object.

// Create a new dataset that will hold medical imaging data
Aspose.Medical.Dicom.Dataset dataset = [];
dataset.AddOrUpdate(Aspose.Medical.Dicom.Tags.Tag.SOPClassUID,
    Aspose.Medical.Dicom.Uid.SecondaryCaptureImageStorage.Code);
dataset.AddOrUpdate(Aspose.Medical.Dicom.Tags.Tag.SOPInstanceUID, "1.2.826.0.1.3680043.10.100.1");

// Create a new DICOM file from the dataset
Aspose.Medical.Dicom.DicomFile dicomFile = new(dataset);

// Add data to the newly created DICOM file
dicomFile.Dataset.AddOrUpdate(Aspose.Medical.Dicom.Tags.Tag.XAAcquisitionFrameRate, 17.95);

Creating a DICOM File with Custom Metadata and Dataset

If you need to customize the metadata along with the dataset, use the constructor that accepts Aspose.Medical.Dicom.MetaInformation and Aspose.Medical.Dicom.Dataset.

// Create a new dataset for storing DICOM attributes
Aspose.Medical.Dicom.Dataset dataset = [];

// Create meta information for the DICOM file
Aspose.Medical.Dicom.MetaInformation metaInfo = [];

// Create a DICOM file with custom metadata and dataset
Aspose.Medical.Dicom.DicomFile dicomFile = new(metaInfo, dataset);

// Add data to the newly created DICOM file
dicomFile.Dataset.AddOrUpdate(Aspose.Medical.Dicom.Tags.Tag.XAAcquisitionFrameRate, 17.95);