Search code examples
c#tagsfo-dicom

The number of frames tag appears in the dataset but is not in the DICOMDIR C#


I add dicom files using the AddFile(dicomFile,name) method but the number of frames tag does not appear.

var sourcePath = Path.Combine(tempDirectory, "DICOM", $"PATIENT{i + 1}", $"STUDY{j + 1}", $"SERIES{k + 1}", $"SUBSERIES{l + 1}");       
var dicomDir = new DicomDirectory { AutoValidate = false };

            foreach (var file in new DirectoryInfo(tempDirectory).GetFiles("*.*", SearchOption.AllDirectories))
            {
                try
                {
                    var dicomFile = DicomFile.Open(file.FullName);

                    if (dicomFile != null)
                    {
                     

                        var referenceField = file.FullName.Replace(tempDirectory, string.Empty).Trim('\\');
                        dicomDir.AddFile(dicomFile, referenceField);
                   
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, ex.Message);
                }
            }

            var dicomDirPath = Path.Combine(tempDirectory, "DICOMDIR");
            dicomDir.Save(dicomDirPath);
            resultDirectories.Add(dicomDirPath);

I also tried the addorupdate method but it doesn't work.
I use the fo-dicom library 4.0.7


Solution

  • When building a DICOMDIR with fo-dicom by iterative calling AddFile for each file, then you will get a DICOMDIR with all the required DicomTags. But of course there are a lot of tags that are optional and you can add them yourself. The method AddFile returns an instance of type DicomDirectoryEntry, which gives you a reference to the patient record entry, the study record entry, the series record entry and the instance record entry. There you can add as many additional optional data that you wish. In your case it would look like

                   [...]
                   if (dicomFile != null)
                   {
                       var referenceField = file.FullName.Replace(tempDirectory, string.Empty).Trim('\\');
                       var entries = dicomDir.AddFile(dicomFile, referenceField);
    
                       // now you can add some additional data.
                       // but before adding values, make sure that those values are available
                       // in your original DicomFile to avoid NullReferenceExceptions. 
                       if (dicomFile.Dataset.Contains(DicomTag.NumberOfFrames))
                       {
                           entries.InstanceRecord.AddOrUpdate(DicomTag.NumberOfFrames, dicomFile.Dataset.GetSingleValue<int>(DicomTag.NumberOfFrames));
                       }
                    }