Beispiel #1
0
        private MemoryStream ExportToDICOMDIR(ref ZipFile zip, List <DicomDataSet> datasets, ExportOptions options)
        {
            DicomDir     dir;
            string       dirDestination = string.Empty;
            string       folderPath;
            MemoryStream zipStream = new MemoryStream();

            dirDestination = GetDestinationFolder();
            if (!Directory.Exists(dirDestination))
            {
                Directory.CreateDirectory(dirDestination);
            }
            dir        = new DicomDir(dirDestination);
            folderPath = Path.Combine(dirDestination, @"DICOM\");
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            try
            {
                Dictionary <string, int> studies = new Dictionary <string, int>();
                Dictionary <string, int> series  = new Dictionary <string, int>();
                int nextStudy  = 0;
                int nextSeries = 0;

                ExportExtensions.ResetAnonymization();
                foreach (DicomDataSet dataset in datasets)
                {
                    string studyInstance  = dataset.GetValue <string>(DicomTag.StudyInstanceUID, string.Empty);
                    string seriesInstance = dataset.GetValue <string>(DicomTag.SeriesInstanceUID, string.Empty);
                    string sopInstance    = dataset.GetValue <string>(DicomTag.SOPInstanceUID, string.Empty);
                    string fileName       = sopInstance + ".dcm";
                    string path           = GetFolderName(studies, series, ref nextStudy, ref nextSeries, studyInstance, seriesInstance);
                    if (zip == null)
                    {
                        zip = new ZipFile();
                    }

                    if (options.FileFormat.ToLower() == "dicomzip" && !string.IsNullOrEmpty(options.DczPassword))
                    {
                        zip.Password = options.DczPassword;
                    }

                    if (options.Anonymize)
                    {
                        dataset.Anonymize();
                    }
                    dataset.Save(folderPath + fileName, DicomDataSetSaveFlags.MetaHeaderPresent);
                    zip.AddEntry(@"DICOM\" + fileName, dataset.ToStream());
                    dir.InsertDataSet(dataset, folderPath + fileName);
                }

                dir.Save();
                if (zip != null)
                {
                    zip.AddFile(dirDestination + "DICOMDIR", string.Empty);
                    if (options.IncludeViewer)
                    {
                        IncludeViewer(ref zip);
                    }
                    zip.Save(zipStream);
                    zip.Dispose();
                }
                zipStream.Position = 0;
            }
            finally
            {
                if (!string.IsNullOrEmpty(dirDestination))
                {
                    if (Directory.Exists(dirDestination))
                    {
                        Directory.Delete(dirDestination, true);
                    }
                }
            }
            return(zipStream);
        }