VintaSoft Imaging .NET SDK v8.7
In This Topic
    DICOM: Annotate DICOM images
    In This Topic

    Annotations of DICOM file are stored in a DICOM Presentation State file. DicomAnnotationCodec class allows to load and save annotations to DICOM Presentation State file.

    At the moment the DicomAnnotationCodec class supports the following annotations:


    Here is an example that demonstrates how to load annotations of DICOM file, change the annotations and save the changed annotations to the source file:
    ' The project, which uses this code, must have references to the following assemblies:
    ' - Vintasoft.Imaging.Annotation.Dicom
    ' - Vintasoft.Imaging.Dicom
    
    ''' <summary>
    ''' Loads DICOM annotations from file,
    ''' changes the text of text annotations,
    ''' saves DICOM annotation back to the source file.
    ''' </summary>
    ''' <param name="presentationStateFilePath">The path to a presentation state file.</param>
    ''' <param name="textValue">The value of text annotations.</param>
    Public Shared Sub ChangeTextAnnotations(presentationStateFilePath As String, textValue As String)
            ' load presentation state file
            Using dicomFile As New Vintasoft.Imaging.Codecs.ImageFiles.Dicom.DicomFile(presentationStateFilePath, False)
                    ' create codec of DICOM annotations
                    Dim annotationCodec As New Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationCodec()
                    ' get annotations from the presentation state file
                    Dim annotationDataCollections As Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationDataCollection() = annotationCodec.Decode(dicomFile.Annotations)
    
                    ' for each annotation collection
                    For Each annotationDataCollection As Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationDataCollection In annotationDataCollections
                            ' for each annotation in annotation collection
                            For Each annotationData As Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationData In annotationDataCollection
                                    ' if annotation is text annotation
                                    If TypeOf annotationData Is Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData Then
                                            ' get text annotation data
                                            Dim textAnnotationData As Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData = DirectCast(annotationData, Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData)
                                            ' change text of text annotation
                                            textAnnotationData.UnformattedTextValue = textValue
                                    End If
                            Next
                    Next
    
                    ' save annotations in the source presentation state file
                    annotationCodec.Encode(dicomFile.Annotations, annotationDataCollections)
    
                    ' save changes in DICOM file
                    dicomFile.SaveChanges()
            End Using
    End Sub
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Imaging.Annotation.Dicom
    // - Vintasoft.Imaging.Dicom
    
    /// <summary>
    /// Loads DICOM annotations from file,
    /// changes the text of text annotations,
    /// saves DICOM annotation back to the source file.
    /// </summary>
    /// <param name="presentationStateFilePath">The path to a presentation state file.</param>
    /// <param name="textValue">The value of text annotations.</param>
    public static void ChangeTextAnnotations(string presentationStateFilePath, string textValue)
    {
        // load presentation state file
        using (Vintasoft.Imaging.Codecs.ImageFiles.Dicom.DicomFile dicomFile = 
            new Vintasoft.Imaging.Codecs.ImageFiles.Dicom.DicomFile(presentationStateFilePath, false))
        {
            // create codec of DICOM annotations
            Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationCodec annotationCodec = 
                new Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationCodec();
            // get annotations from the presentation state file
            Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationDataCollection[] annotationDataCollections = 
                annotationCodec.Decode(dicomFile.Annotations);
    
            // for each annotation collection
            foreach (Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationDataCollection annotationDataCollection in annotationDataCollections)
            {
                // for each annotation in annotation collection
                foreach (Vintasoft.Imaging.Annotation.Dicom.DicomAnnotationData annotationData in annotationDataCollection)
                {
                    // if annotation is text annotation
                    if (annotationData is Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData)
                    {
                        // get text annotation data
                        Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData textAnnotationData = 
                            (Vintasoft.Imaging.Annotation.Dicom.DicomTextAnnotationData)annotationData;
                        // change text of text annotation
                        textAnnotationData.UnformattedTextValue = textValue;
                    }
                }
            }
    
            // save annotations in the source presentation state file
            annotationCodec.Encode(dicomFile.Annotations, annotationDataCollections);
    
            // save changes in DICOM file
            dicomFile.SaveChanges();
        }
    }