Search code examples
c#dicomfo-dicom

DICOMDir file getting corrupted after save with fo-dicom


I'm experiencing an issue with adding a file to a DICOMDir. Based on this example I've successfully created and saved to disk an image from a series. Then, I tried adding that file to the DICOMDIR, so that the Dir references the new file, and, though the saving is successful, when I try to open the DICOMDir and its series again, I get a "Tag: (0088,0200) not found in dataset" exception.

Code is as follows:

var dataset = new DicomDataset();
this.FillDataset(dataset); //this function copies several Tag values of an already existing DICOM Series file, such as Patient information
dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);
dataset.Add(DicomTag.Rows, (ushort)rows);
dataset.Add(DicomTag.Columns, (ushort)columns);

var pixelData = DicomPixelData.Create(dataset, true);
pixelData.AddFrame(buffer);
var dicomfile = new DicomFile(dataset);
var pathImage = Path.Combine(dirImages.FullName, imageFileName);
dicomfile.Save(pathImage); //Image is saved fine and it's well formed, I've checked opening it with an online DICOM viewer

var dicomdirPath = Path.Combine(studyPath, Constants.DICOMDIRFileName);
var dicomdir = DicomDirectory.Open(dicomdirPath);
dicomdir.AddFile(dicomfile, $@"Images\{imageFileName}");
dicomdir.Save(dicomdirPath); //this executes without problems and the DICOMDIR is saved

And this is the series opening method:

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)
            {
                var dicomDataset = imageRecord.GetSequence(DicomTag.IconImageSequence).Items.First(); //This line works fine before saving the image in the method above, but throws when opening the same study
                //Load data and series from dataset
            }
        }
    }
}

I don't know if I'm missing something regarding saving a DICOMDir file, or if it's an error.


Solution

  • You try to access the IconImageSequence (0088,0200) that is obviously not present. DicomDir does only contain some main data of the image. When Adding an image to the dicomdir it is up to you to add additional information. One of those optional informations, that fo-dicom does not automatically add, is the Icon. DicomDir allows to contain a small icon to show if you want to display some previews quickly.

    Actually imageRecord should contain all the informations you might need like instanceuid or filename etc.

    I don't know why the line of code worked well before you stored the file with fo-dicom. I assume there already was a DICOMDIR created with some other application that included the Icon? then the foreach crashes when you reach the newly added entry.

    You could either add an Icon yourself when adding the new instance to the DICOMDIR, or you could add a check like "if imageRecord.TryGetSequece(iconImageSequence, out seq).." to handle the cases where there are no icons.

    I recommend to add the check anyway, because you might read a DICOMDIR with a reference to some strucured report some day and those structured reports do not have pixel data and therefore will not have an icon included.