The DicomDirOps example demonstates how to use DCF to perform file set operations related to DICOMDIRs.
Classes
Class | Description | |
---|---|---|
Program |
This example demonstrates how to create a new DICOMDIR, and how to update an existing one.
|
Examples
DicomDirOps Sample Code
public class Program { private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); /// <summary> /// Main entry point for DicomDirOps. /// </summary> [STAThread] public static void Main() { try { string assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (String.IsNullOrEmpty(assemblyLocation)) assemblyLocation = "."; string imagesDir = Path.Combine(assemblyLocation, "ExampleDicomDir"); string study1ImagesDir = Path.Combine(imagesDir, "Study1"); string study2ImagesDir = Path.Combine(imagesDir, "Study2"); string dicomDirFilePath = Path.Combine(imagesDir, "DICOMDIR"); // Delete the existing DICOMDIR file if (File.Exists(dicomDirFilePath)) { File.Delete(dicomDirFilePath); } // Create the DicomDirectoryRecord using the given directory tree and save to disk DicomDir dicomDir = FileSetCreate(study1ImagesDir); Logger.InfoFormat("Dicom Directory Record:{0}{1}", Environment.NewLine, dicomDir.RootDir); dicomDir.Save(dicomDirFilePath); // Now update the existing DicomDirectoryRecord using the given directory DicomDir updatedDicomDir = FileSetUpdate(dicomDirFilePath, study2ImagesDir); Logger.InfoFormat("Updated Dicom Directory Record:{0}{1}", Environment.NewLine, updatedDicomDir.RootDir); updatedDicomDir.Save(dicomDirFilePath); } catch (Exception e) { Console.WriteLine("Exception caught during execution: {0}", e); } if (Debugger.IsAttached) { Console.Write("Press any key to continue . . . "); Console.ReadKey(); } } /// <summary> /// Creates a new Dicom Directory Record from the list of files contained in the given input directory. /// </summary> /// <param name="imageDir">The directory containing the images to add to the directory record.</param> /// <returns>The Dicom Directory Record containing information about the containing files from the input /// directory path, or null if there was an error.</returns> private static DicomDir FileSetCreate(string imageDir) { DicomDir dicomDir = new DicomDir(); DirectoryInfo di = new DirectoryInfo(imageDir); // Our example Dicom files have the '.dcm' extension, but this is by no means mandatory. foreach (FileInfo file in di.GetFiles("*", SearchOption.AllDirectories)) { // ignore any .txt file in the directory if (file.Extension == ".txt") continue; using (DicomFileInput dfi = new DicomFileInput(file.FullName)) { DicomDataSet imageDs = dfi.ReadDataSetNoPixels(); DicomDirectoryRecord root = dicomDir.RootDir; PatientDirectoryRecord patient = PatientDirectoryRecord.Find(root, imageDs, true, true); StudyDirectoryRecord study = StudyDirectoryRecord.Find(patient, imageDs, true, true); SeriesDirectoryRecord series = SeriesDirectoryRecord.Find(study, imageDs, true, true); if (ImageDirectoryRecord.Exists(series, imageDs)) { throw new InvalidOperationException("a matching image record already exists under that series"); } ImageDirectoryRecord image = ImageDirectoryRecord.Create(series, imageDs, true); image.ReferencedFileId = file.Name; } } return dicomDir; } private static DicomDir FileSetUpdate(string dicomDirFilePath, string dir) { DicomDir dicomDir = new DicomDir(dicomDirFilePath); DirectoryInfo di = new DirectoryInfo(dir); // Our example Dicom files have the '.dcm' extension, but this is by no means mandatory. foreach (FileInfo file in di.GetFiles("*", SearchOption.AllDirectories)) { // ignore any .txt file in the directory if (file.Extension == ".txt") continue; using (DicomFileInput dfi = new DicomFileInput(file.FullName)) { DicomDataSet imageDs = dfi.ReadDataSetNoPixels(); DicomDirectoryRecord root = dicomDir.RootDir; PatientDirectoryRecord patient = PatientDirectoryRecord.Find(root, imageDs, true, true); StudyDirectoryRecord study = StudyDirectoryRecord.Find(patient, imageDs, true, true); SeriesDirectoryRecord series = SeriesDirectoryRecord.Find(study, imageDs, true, true); if (ImageDirectoryRecord.Exists(series, imageDs)) { throw new InvalidOperationException("a matching image record already exists under that series"); } ImageDirectoryRecord image = ImageDirectoryRecord.Create(series, imageDs, true); image.ReferencedFileId = file.Name; } } return dicomDir; }