Пример #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);
        }
Пример #2
0
        public Stream ExportAllSeries(string userName, string patientID, ExportOptions options)
        {
            MemoryStream zipStream = new MemoryStream();
            ZipFile      zip       = null;

            try
            {
                if (options.FileFormat.ToLower() == "dicomzip" && string.IsNullOrEmpty(options.DczPassword))
                {
                    throw new ArgumentException("Must pass a password when creating a DICOM Zip");
                }

                if (options.FileFormat.ToLower() == "dicomgray" || options.FileFormat.ToLower() == "dicomzip")
                {
                    List <DicomDataSet> datasets = _Exporter.GetAllPatientDataSets(patientID);

                    if (options.CreateDICOMDIR)
                    {
                        zipStream = ExportToDICOMDIR(ref zip, datasets, options);
                    }
                    else
                    {
                        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();
                            }
                            zip.AddEntry(path + fileName, dataset.ToStream());
                        }

                        if (zip != null)
                        {
                            if (options.IncludeViewer)
                            {
                                IncludeViewer(ref zip);
                            }

                            zip.Save(zipStream);
                            zip.Dispose();
                        }
                        zipStream.Position = 0;
                    }
                }
                else
                {
                    List <SeriesImage> seriesImages = _Exporter.GetAllPatientImages(patientID);

                    zip = LoadInstances(seriesImages, userName, options);
                    if (zip != null)
                    {
                        zip.Save(zipStream);
                        zip.Dispose();
                    }
                    zipStream.Position = 0;
                }

                return(zipStream);
            }
            finally
            {
            }
        }