Working with DICOM Data Elements Using Aspose.Medical

Working with DICOM Data Elements Using Aspose.Medical

This documentation provides a guide on managing DICOM data elements using the Aspose.Medical.Dicom.Elements.IElement API. The focus is on adding, updating, removing, and replacing data for DICOM data elements.

Note: This guide uses FloatingPointDouble (an implementation of IElement that works with double-precision floating-point values) as an example. Aspose.Medical provides the following classes that matches with the VR (Value Representation) defined in the DICOM specification. All these classes can be found in Aspose.Medical.Dicom.Elements namespace.

Retrieving Data from a DICOM Element

Retrieving data from an element can be done using several approaches provided by the API.

  • Get<T>(int index): Retrieves a specific value by position.
  • Get<T>(Index index): Retrieves a specific value by position.
  • GetValues<T>(): Retrieves all stored values.
  • GetOrDefault<T>(int index): Retrieves a value at a specified index or returns a default value if the index is out of range.

The following snippet demonstrates how the listed methods work.

// Create a DICOM Tag for the element
Aspose.Medical.Dicom.Tags.Tag tag = Tag.TableOfYBreakPoints;

// Initialize FloatingPointDouble with a value
Aspose.Medical.Dicom.Elements.FloatingPointDouble element = new(tag, [50.1, 100.2, 150.3]);

// Retrieve a value by numeric index
double firstValue = element.Get<float>(0);
Console.WriteLine("First Value: " + firstValue); // 50.1

// Retrieve a value by index
double lastValue = element.Get<double>(^1);
Console.WriteLine("Last Value: " + lastValue); // 150.3

// Retrieve all values
System.Span<double> allValues = element.GetValues<double>();
foreach (double val in allValues)
{
    Console.WriteLine("Value: " + val);
}

// Retrieve a value at a specific position safely
double defaultValue = element.GetOrDefault<double>(4);
Console.WriteLine("Value (Safe Retrieval): " + defaultValue); // default(double), e.g., 0

Manage Data of a DICOM Element

Although Aspose.Medical.Dicom.Elements.IElement does not provide methods to modify element data, such methods are typically available at the element type level. This approach helps prevent common mistakes related to data manipulation.

Adding Data to a DICOM Element

You can use the Add or AddRange methods of FloatingPointDouble to append values to a DICOM element.

To add a single value to an element:

// Create a DICOM Tag for the element
Aspose.Medical.Dicom.Tags.Tag tag = Tag.TableOfYBreakPoints;

// Initialize FloatingPointDouble with a value
Aspose.Medical.Dicom.Elements.FloatingPointDouble element = new(tag, []);

// The element now contains one floating-point value: 42.5
element.Add(42.5);

To add multiple values to an element at once:

// Create a DICOM Tag for the element
Aspose.Medical.Dicom.Tags.Tag tag = Tag.TableOfYBreakPoints;

// Initialize FloatingPointDouble with a value
Aspose.Medical.Dicom.Elements.FloatingPointDouble element = new(tag, []);

double[] values = [10.1, 20.2, 30.3];
// The element now contains values: [10.1, 20.2, 30.3]
element.AddRange(values);

Inserting Data into a DICOM Element

You can insert a value at a specific index using the Insert method.

// Create a DICOM Tag for the element
Aspose.Medical.Dicom.Tags.Tag tag = Tag.TableOfYBreakPoints;

// Initialize FloatingPointDouble with a value
Aspose.Medical.Dicom.Elements.FloatingPointDouble element = new(tag, [1.1, 2.2, 4.4]);

// The element now contains values: [1.1, 2.2, 3.3, 4.4]
element.Insert(2, 3.3);

Removing Data from a DICOM Element

You can remove values from an element using Remove or RemoveAt.

The Remove method deletes the first occurrence of a specific value.

// Create a DICOM Tag for the element
Aspose.Medical.Dicom.Tags.Tag tag = Tag.TableOfYBreakPoints;

// Initialize FloatingPointDouble with a value
Aspose.Medical.Dicom.Elements.FloatingPointDouble element = new(tag, [5.5, 6.6, 7.7, 6.6]);

// The element now contains values: [5.5, 7.7, 6.6]
element.Remove(6.6);

The RemoveAt method removes the value at a specified index.

// Create a DICOM Tag for the element
Aspose.Medical.Dicom.Tags.Tag tag = Tag.TableOfYBreakPoints;

// Initialize FloatingPointDouble with a value
Aspose.Medical.Dicom.Elements.FloatingPointDouble element = new(tag, [11.1, 22.2, 33.3]);

// The element now contains values: [11.1, 33.3]
element.RemoveAt(1);

Replacing Data in a DICOM Element

The Replace method allows resetting the element’s values with a new collection.

// Create a DICOM Tag for the element
Aspose.Medical.Dicom.Tags.Tag tag = Tag.TableOfYBreakPoints;

// Initialize FloatingPointDouble with a value
Aspose.Medical.Dicom.Elements.FloatingPointDouble element = new(tag, [100.1, 200.2, 300.3]);

// The element now contains values: [400.4, 500.5]
element.Replace([400.4, 500.5 ]);