Search code examples
c#dicomfo-dicom

Getting "DicomTag doesn't support values" when trying to obtain DicomDataset


After upgrading to fo-dicom 4.0, I started to get a DicomDataException: 'DicomTag doesn't support values' when trying to obtain a DicomDataset from a DICOMDIR file.

Code is as follows:

var dicomDirectory = await DicomDirectory.OpenAsync(dicomdirPath);
foreach (var patientRecord in dicomDirectory.RootDirectoryRecordCollection)
{
    foreach (var studyRecord in patientRecord.LowerLevelDirectoryRecordCollection)
    {
        foreach (var seriesRecord in studyRecord.LowerLevelDirectoryRecordCollection)
        {
            foreach (var imageRecord in seriesRecord.LowerLevelDirectoryRecordCollection)
            {
                //this is the problematic line
                var dicomDataset = imageRecord.GetValue<DicomSequence>(DicomTag.IconImageSequence, 0).Items.First();
                //more stuff
            }
        }
    }
}

With the previous version (3.?) I was doing var dicomImage = imageRecord.Get<DicomSequence>(DicomTag.IconImageSequence).Items.First(); and it worked just fine, but after the upgrade I was getting an Obsolete warning so I changed it to the recommended method, which was GetValue.

How can I get the dataset using the current version of fo-dicom?


Solution

  • Finally, after digging through fo-dicom's Gitter and GitHub issues (and as @BenVoight mentioned in the comments as well), I found that in 4.0 we should use GetSequence(DicomTag.IconImageSequence) instead of GetValue<DicomSequence>(DicomTag.IconImageSequence): when retrieving Sequences, using GetValue will throw. Several other Get methods have been added, such as GetValueOrDefault, GetValues<T> instead of GetValue<T[]>, and more, as can be seen in the project's Dataset test.

    Also, as a corollary, I also found another API incompatibility in 4.0 regarding the previous version (I'll post it here in case someone upgraded and got lost in the changes): when dealing with DicomImage, we shouldn't access its .Dataset to retrieve values, because it's been deprecated as well. Instead, we should store a reference to the Dataset from which the DicomImage was created in order to retrieve tag values.