protected override void Dispose(bool disposing) { if (disposing) { _frame = null; _dicomFile = null; if (_imageSop != null) { _imageSop.Dispose(); _imageSop = null; } if (_sopDataSource != null) { _sopDataSource.Dispose(); _sopDataSource = null; } if (_filename != null) { if (File.Exists(_filename)) File.Delete(_filename); _filename = null; } } base.Dispose(disposing); }
/// <summary> /// Creates a new instance of <see cref="Sop"/> from a local file. /// </summary> /// <param name="filename">The path to a local DICOM Part 10 file.</param> public Sop(string filename) { ISopDataSource dataSource = new LocalSopDataSource(filename); try { Initialize(dataSource); } catch { dataSource.Dispose(); throw; } }
public MockDicomPresentationImage(string filename) : base(new GrayscaleImageGraphic(10, 10)) { if (Path.IsPathRooted(filename)) _filename = filename; else _filename = Path.Combine(Environment.CurrentDirectory, filename); _dicomFile = new DicomFile(); _dicomFile.DataSet[DicomTags.SopClassUid].SetStringValue(SopClass.SecondaryCaptureImageStorageUid); _dicomFile.DataSet[DicomTags.SopInstanceUid].SetStringValue(DicomUid.GenerateUid().UID); _dicomFile.MetaInfo[DicomTags.MediaStorageSopClassUid].SetStringValue(_dicomFile.DataSet[DicomTags.SopClassUid].ToString()); _dicomFile.MetaInfo[DicomTags.MediaStorageSopInstanceUid].SetStringValue(_dicomFile.DataSet[DicomTags.SopInstanceUid].ToString()); _dicomFile.Save(_filename); _sopDataSource = new LocalSopDataSource(_dicomFile); _imageSop = new ImageSop(_sopDataSource); _frame = new MockFrame(_imageSop, 1); }
public List<BitmapSource> ReturnImageListFromFile(string filePath) { List<BitmapSource> bmpImageList = new List<BitmapSource>(); LocalSopDataSource dicomDataSource = new LocalSopDataSource(filePath); ImageSop imageSop = new ImageSop(dicomDataSource); IEnumerable images = PresentationImageFactory.Create(imageSop).ToArray(); int i = 1; foreach (IPresentationImage presentationImage in images) { int width = imageSop.Frames[i].Columns; int height = imageSop.Frames[i].Rows; Bitmap bmp = presentationImage.DrawToBitmap(width, height); dicomImages.Add(new DicomImage { Bitmap = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(width, height)) }); i++; } return bmpImageList; }
public void HandleAllGetRequests(HttpListenerContext context) { var rc = new ResponseClass(); string studyuid = context.Request.QueryString["studyuid"]; string seriesuid = context.Request.QueryString["seriesuid"]; string check = context.Request.QueryString["check"]; if (!string.IsNullOrEmpty(check)) { rc.Success = true; rc.Message = ""; rc.Data = true; string json = Newtonsoft.Json.JsonConvert.SerializeObject(rc); this.SendTextResponse(context, json); return; } var node = ADCM.GetSelectedNode(); try { LOG.Write("New request"); if (string.IsNullOrEmpty(studyuid) || string.IsNullOrEmpty(seriesuid)) { throw new Exception("No studyuid or seriesuid provided"); } bool downloaded = ADCM.DownloadOneSeries(studyuid, seriesuid); if (!downloaded) { throw new Exception("Unable to download study"); } string seriesPath = Path.Combine(node.LocalStorage, studyuid, seriesuid); if (!Directory.Exists(seriesPath)) { throw new Exception("Series path not found: " + seriesPath); } var dcmFiles = Directory.GetFiles(seriesPath, "*.dcm"); string filetouse = null; decimal mid = dcmFiles.Length / 2; int index = (int)Math.Ceiling(mid); for (int i = index; i < dcmFiles.Length; i++) { var dcm = dcmFiles[i]; ClearCanvas.Dicom.DicomFile dcmFile = new ClearCanvas.Dicom.DicomFile(dcm); dcmFile.Load(); ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource localds = new ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource(dcmFile); if (!localds.IsImage) { continue; } else { filetouse = dcm; break; } } if (string.IsNullOrEmpty(filetouse)) { for (int i = 0; i < dcmFiles.Length; i++) { var dcm = dcmFiles[i]; ClearCanvas.Dicom.DicomFile dcmFile = new ClearCanvas.Dicom.DicomFile(dcm); dcmFile.Load(); ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource localds = new ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource(dcmFile); if (!localds.IsImage) { continue; } else { filetouse = dcm; break; } } } if (string.IsNullOrEmpty(filetouse)) { throw new Exception("Unable to find image in downloaded DICOM files"); } if (!File.Exists(filetouse)) { throw new Exception("Unable to find DICOM file to use"); } string base64String = Convert.ToBase64String(AIMG.GetImageBytesFromDcm(filetouse)); base64String = "data:image/jpeg;base64," + base64String; rc.Success = true; rc.Message = ""; rc.Data = base64String; string json = Newtonsoft.Json.JsonConvert.SerializeObject(rc); this.SendTextResponse(context, json); } catch (Exception ex) { LOG.Write("ERROR: " + ex.Message); rc.Data = null; rc.Success = false; rc.Message = ex.Message; string json = Newtonsoft.Json.JsonConvert.SerializeObject(rc); this.SendTextResponse(context, json); } finally { string studypath = Path.Combine(node.LocalStorage, studyuid); if (Directory.Exists(studypath)) { Directory.Delete(studypath, true); } } }
private static DisposableList<IPresentationImage> CreateImages(DicomFile dicomFile) { using (var dataSource = new LocalSopDataSource(dicomFile)) { using (var sop = new ImageSop(dataSource)) { return new DisposableList<IPresentationImage>(PresentationImageFactory.Create(sop)); } } }
public static bool IsValid(TreeNode node) { try { LocalSopDataSource _dicomDataSource = new LocalSopDataSource(((FileInfo)node.Tag).FullName); ImageSop isop = new ImageSop(_dicomDataSource); //DicomFile dcf = new DicomFile(((FileInfo)node.Tag).FullName); //dcf.Load(DicomReadOptions.DoNotStorePixelDataInDataSet); //00:00:01.5625000 //00:00:01.7625000 //00:00:00.0468750 } catch (Exception ex) { return false; } return true; }
public static bool ConvertDcmToPng(DbStudy study) { try { string dcmPath = ADCM.GetStoreString(); string studyPath = Path.Combine(dcmPath, study.study_uid); if (!Directory.Exists(studyPath)) throw new Exception("Study path not found"); var allSeriesPaths = Directory.GetDirectories(studyPath); if (allSeriesPaths.Length < 1) throw new Exception("No series subdirectories"); foreach (var s in allSeriesPaths) { var dcmFiles = Directory.GetFiles(s, "*.dcm"); if (dcmFiles.Length < 1) throw new Exception("No DCM files inside series path: " + s); DicomFile tempdcm = new DicomFile(dcmFiles[0]); tempdcm.Load(); var seriesName = tempdcm.DataSet[DicomTags.SeriesDescription].GetString(0, null); var seriesUID = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1); if (string.IsNullOrEmpty(seriesName)) seriesName = "Unamed_Series"; APetaPoco.SetConnectionString("cn1"); var bm = APetaPoco.PpRetrieveOne<DbSeries>("Series", "[case_id] = '" + study.case_id + "' AND [series_uid] = '" + seriesUID + "'"); DbSeries dbseries = null; if (bm.Success) { dbseries = (DbSeries)bm.Data; } string outputPath = Path.Combine(dcmPath, "OUTPUT", study.case_id, study.study_uid, seriesUID); if (!Directory.Exists(outputPath)) Directory.CreateDirectory(outputPath); //int fileCount = 0; for (int k = 0; k < dcmFiles.Length; k++) { DicomFile dcmFile = new DicomFile(dcmFiles[k]); dcmFile.Load(); int fileCount = 0; var windowWidth = dcmFile.DataSet[DicomTags.WindowWidth].ToString(); if (!string.IsNullOrEmpty(windowWidth)) { var tempSplitString = windowWidth.Split('\\'); windowWidth = tempSplitString[0]; } var windowCenter = dcmFile.DataSet[DicomTags.WindowCenter].ToString(); if (!string.IsNullOrEmpty(windowCenter)) { var tempSplitString = windowCenter.Split('\\'); windowCenter = tempSplitString[0]; } var ww = dcmFile.DataSet[DicomTags.WindowWidth].GetFloat32(0, 0); var wc = dcmFile.DataSet[DicomTags.WindowCenter].GetFloat32(0, 0); if (ww == 0 && !string.IsNullOrEmpty(windowWidth)) { if (windowWidth.Contains(".")) { var tempSplitString = windowWidth.Split('.'); ww = int.Parse(tempSplitString[0]); } else { ww = int.Parse(windowWidth); } } if (wc == 0 && !string.IsNullOrEmpty(windowCenter)) { if (windowCenter.Contains(".")) { var tempSplitString = windowCenter.Split('.'); wc = int.Parse(tempSplitString[0]); } else { wc = int.Parse(windowCenter); } } LocalSopDataSource localds = new LocalSopDataSource(dcmFile); if (!localds.IsImage) { continue; } ImageSop sop = new ImageSop(localds); int frameCount = sop.Frames.Count; var fileName = dcmFile.DataSet[DicomTags.InstanceNumber].GetInt16(0, 0); if (frameCount > 1) { for (int j = 1; j <= frameCount; j++) { GC.Collect(); Frame f = sop.Frames[j]; var jpgPath = Path.Combine(outputPath, fileName + "." + j + ".png"); Bitmap bmp = null; if (string.IsNullOrEmpty(windowWidth) || string.IsNullOrEmpty(windowCenter)) { bmp = DrawDefaultFrame(f); } else { bmp = DrawLutFrame(f, ww, wc); } if (bmp != null) { if(dbseries != null && dbseries.crop_h != null && dbseries.crop_w != null && dbseries.crop_x != null && dbseries.crop_y != null) { bmp = Crop(bmp, dbseries.crop_x.Value, dbseries.crop_y.Value, dbseries.crop_w.Value, dbseries.crop_h.Value); } SaveImage(bmp, jpgPath); } fileCount += 1; GC.Collect(); } } else { GC.Collect(); var jpgPath = Path.Combine(outputPath, fileName + ".png"); Frame f = sop.Frames[1]; Bitmap bmp = null; if (string.IsNullOrEmpty(windowWidth) || string.IsNullOrEmpty(windowCenter)) { bmp = DrawDefaultFrame(f); } else { bmp = DrawLutFrame(f, ww, wc); } if (bmp != null) { if (dbseries != null && dbseries.crop_h != null && dbseries.crop_w != null && dbseries.crop_x != null && dbseries.crop_y != null) { bmp = Crop(bmp, dbseries.crop_x.Value, dbseries.crop_y.Value, dbseries.crop_w.Value, dbseries.crop_h.Value); } SaveImage(bmp, jpgPath); } fileCount += 1; GC.Collect(); } } } LOG.InsertEvent("Successfully converted study from DCM to PNG", "IMG", null, study.case_id, study.study_uid); return true; } catch (Exception ex) { string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name; LOG.Write(errorString); LOG.Write(ex.Message); LOG.InsertEvent(errorString, "IMG", ex.Message, study.case_id, study.study_uid); return false; } }
public static byte[] GetImageBytesFromDcm(string dcmPath) { DicomFile dcmFile = new DicomFile(dcmPath); dcmFile.Load(); int fileCount = 0; var windowWidth = dcmFile.DataSet[DicomTags.WindowWidth].ToString(); if (!string.IsNullOrEmpty(windowWidth)) { var tempSplitString = windowWidth.Split('\\'); windowWidth = tempSplitString[0]; } var windowCenter = dcmFile.DataSet[DicomTags.WindowCenter].ToString(); if (!string.IsNullOrEmpty(windowCenter)) { var tempSplitString = windowCenter.Split('\\'); windowCenter = tempSplitString[0]; } var ww = dcmFile.DataSet[DicomTags.WindowWidth].GetFloat32(0, 0); var wc = dcmFile.DataSet[DicomTags.WindowCenter].GetFloat32(0, 0); if (ww == 0 && !string.IsNullOrEmpty(windowWidth)) { if (windowWidth.Contains(".")) { var tempSplitString = windowWidth.Split('.'); ww = int.Parse(tempSplitString[0]); } else { ww = int.Parse(windowWidth); } } if (wc == 0 && !string.IsNullOrEmpty(windowCenter)) { if (windowCenter.Contains(".")) { var tempSplitString = windowCenter.Split('.'); wc = int.Parse(tempSplitString[0]); } else { wc = int.Parse(windowCenter); } } LocalSopDataSource localds = new LocalSopDataSource(dcmFile); if (!localds.IsImage) { return null; } ImageSop sop = new ImageSop(localds); int frameCount = sop.Frames.Count; if (frameCount > 1) { int midFrame = Convert.ToInt32(frameCount / 2); GC.Collect(); Frame f = sop.Frames[midFrame]; //var jpgPath = Path.Combine(outputPath, fileName + "." + j + ".png"); Bitmap bmp = null; if (string.IsNullOrEmpty(windowWidth) || string.IsNullOrEmpty(windowCenter)) { bmp = DrawDefaultFrame(f); } else { bmp = DrawLutFrame(f, ww, wc); } if (bmp != null) { return ImageToByte2(bmp); } } else { GC.Collect(); Frame f = sop.Frames[1]; Bitmap bmp = null; if (string.IsNullOrEmpty(windowWidth) || string.IsNullOrEmpty(windowCenter)) { bmp = DrawDefaultFrame(f); } else { bmp = DrawLutFrame(f, ww, wc); } if (bmp != null) { return ImageToByte2(bmp); } fileCount += 1; GC.Collect(); } return null; }
public void HandleAllGetRequests(HttpListenerContext context) { var rc = new ResponseClass(); string studyuid = context.Request.QueryString["studyuid"]; string seriesuid = context.Request.QueryString["seriesuid"]; string check = context.Request.QueryString["check"]; if (!string.IsNullOrEmpty(check)) { rc.Success = true; rc.Message = ""; rc.Data = true; string json = Newtonsoft.Json.JsonConvert.SerializeObject(rc); this.SendTextResponse(context, json); return; } var node = ADCM.GetSelectedNode(); try { LOG.Write("New request"); if (string.IsNullOrEmpty(studyuid) || string.IsNullOrEmpty(seriesuid)) { throw new Exception("No studyuid or seriesuid provided"); } bool downloaded = ADCM.DownloadOneSeries(studyuid, seriesuid); if (!downloaded) { throw new Exception("Unable to download study"); } string seriesPath = Path.Combine(node.LocalStorage, studyuid, seriesuid); if (!Directory.Exists(seriesPath)) { throw new Exception("Series path not found: " + seriesPath); } var dcmFiles = Directory.GetFiles(seriesPath, "*.dcm"); string filetouse = null; decimal mid = dcmFiles.Length / 2; int index = (int)Math.Ceiling(mid); for (int i = index; i < dcmFiles.Length; i++) { var dcm = dcmFiles[i]; ClearCanvas.Dicom.DicomFile dcmFile = new ClearCanvas.Dicom.DicomFile(dcm); dcmFile.Load(); ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource localds = new ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource(dcmFile); if (!localds.IsImage) { continue; } else { filetouse = dcm; break; } } if (string.IsNullOrEmpty(filetouse)) { for (int i = 0; i < dcmFiles.Length; i++) { var dcm = dcmFiles[i]; ClearCanvas.Dicom.DicomFile dcmFile = new ClearCanvas.Dicom.DicomFile(dcm); dcmFile.Load(); ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource localds = new ClearCanvas.ImageViewer.StudyManagement.LocalSopDataSource(dcmFile); if (!localds.IsImage) { continue; } else { filetouse = dcm; break; } } } if(string.IsNullOrEmpty(filetouse)) { throw new Exception("Unable to find image in downloaded DICOM files"); } if (!File.Exists(filetouse)) { throw new Exception("Unable to find DICOM file to use"); } string base64String = Convert.ToBase64String(AIMG.GetImageBytesFromDcm(filetouse)); base64String = "data:image/jpeg;base64," + base64String; rc.Success = true; rc.Message = ""; rc.Data = base64String; string json = Newtonsoft.Json.JsonConvert.SerializeObject(rc); this.SendTextResponse(context, json); } catch (Exception ex) { LOG.Write("ERROR: " + ex.Message); rc.Data = null; rc.Success = false; rc.Message = ex.Message; string json = Newtonsoft.Json.JsonConvert.SerializeObject(rc); this.SendTextResponse(context, json); } finally { string studypath = Path.Combine(node.LocalStorage, studyuid); if (Directory.Exists(studypath)) { Directory.Delete(studypath, true); } } }