protected static IPresentationImage GetImage(ImageKey key) { string filename = string.Format(_testImagePathFormat, key.ToString().ToLower()); try { LocalSopDataSource dataSource = new LocalSopDataSource(filename); ImageSop imageSop = new ImageSop(dataSource); IPresentationImage theOne = null; foreach (IPresentationImage image in PresentationImageFactory.Create(imageSop)) { if (theOne == null) { theOne = image; continue; } image.Dispose(); } imageSop.Dispose(); return(theOne); } catch (Exception ex) { throw new FileNotFoundException("Unable to load requested test image. Please check that the assembly has been built.", filename, ex); } }
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); }
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))); } } }
private static IPresentationImage CreateResultImage(Bitmap bitmap, ColorMode colorMode, string filePath) { DicomFile file = new DicomFile(); file.DataSet[DicomTags.BitsAllocated].SetInt32(0, 8); file.DataSet[DicomTags.BitsStored].SetInt32(0, 8); file.DataSet[DicomTags.HighBit].SetInt32(0, 7); file.DataSet.RemoveAttribute(DicomTags.WindowCenter); file.DataSet.RemoveAttribute(DicomTags.WindowWidth); file.DataSet[DicomTags.PixelSpacing].SetInt32(0, 0); file.DataSet[DicomTags.RescaleIntercept].SetInt32(0, 0); file.DataSet[DicomTags.RescaleSlope].SetInt32(0, 1); file.DataSet[DicomTags.Rows].SetInt32(0, bitmap.Height); file.DataSet[DicomTags.Columns].SetInt32(0, bitmap.Width); file.DataSet[DicomTags.PixelRepresentation].SetInt32(0, 0); file.DataSet[DicomTags.NumberOfFrames].SetInt32(0, 1); file.DataSet[DicomTags.PhotometricInterpretation].SetStringValue("MONOCHROME2"); file.DataSet[DicomTags.SopClassUid].SetStringValue("1.2.840.10008.5.1.4.1.1.7"); file.DataSet[DicomTags.Modality].SetStringValue("OT"); file.DataSet[DicomTags.TransferSyntaxUid].SetStringValue("1.2.840.10008.1.2"); file.DataSet[DicomTags.StudyId].SetStringValue("1"); file.DataSet[DicomTags.StudyInstanceUid].SetStringValue("1.2.276.0.7230010.3.1.2.2866517296.296.1377417571.2"); file.DataSet[DicomTags.SeriesNumber].SetInt32(0, 1); file.DataSet[DicomTags.SamplesPerPixel].SetInt32(0, 1); file.DataSet[DicomTags.SeriesInstanceUid].SetStringValue(" 1.2.276.0.7230010.3.1.3.2866517296.296.1377417571.3"); file.DataSet[DicomTags.SopInstanceUid].SetStringValue(DicomUid.GenerateUid().UID); file.DataSet[DicomTags.PixelData].Values = null; byte[] pixelData = BitMapUtility.GetBitmap(bitmap, colorMode); file.DataSet[DicomTags.PixelData].Values = pixelData; file.MediaStorageSopClassUid = file.DataSet[DicomTags.SopClassUid]; file.MediaStorageSopInstanceUid = file.DataSet[DicomTags.SopInstanceUid]; string fileName = string.Format("{0}\\{1}", filePath, DateTime.Now.Ticks.ToString()); file.Save(fileName); var dataSource = new LocalSopDataSource(file); Sop dstSop = Sop.Create(dataSource); IPresentationImage presentation = (PresentationImageFactory.Create((ImageSop)dstSop))[0]; if (presentation is IAnnotationLayoutProvider) { foreach (AnnotationBox box in ((IAnnotationLayoutProvider)presentation).AnnotationLayout.AnnotationBoxes) { box.Visible = false; } } return(presentation); }
public void GetPixelData(PrintScu.ImageBox imageBox, ColorMode colorMode, out ushort rows, out ushort columns, out byte[] pixelData) { try { LocalSopDataSource dataSource = _file.DataSource as LocalSopDataSource; dataSource.File.Load(DicomReadOptions.Default | DicomReadOptions.StorePixelDataReferences); rows = dataSource.File.DataSet[DicomTags.Rows].GetUInt16(0, 0); columns = dataSource.File.DataSet[DicomTags.Columns].GetUInt16(0, 0); DicomUncompressedPixelData uncompressedPixelData = new DicomUncompressedPixelData(dataSource.File); pixelData = uncompressedPixelData.GetFrame(0); } catch (Exception e) { Platform.Log(LogLevel.Error, string.Format("获得像素数据失败:{0}", e.Message)); throw e; } }
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 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 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); }