public static string GetValueByTag(string strDcm, DicomTag tagname) { DicomImage image = new DicomImage(strDcm); string item_value = DicomUtility.GetStringFromDicomTag(image.Dataset, tagname); return(item_value); }
public static void Bmp2Dcm(string bmpPath, string dcmPath, string patient_name, string sex, string age, string process_num, string modality, string hospital_name, string study_description = "", string studydate = "", int TransferSyntax = -1, string sop_uid = "") { Bitmap bitmap = new Bitmap(bmpPath); bitmap = DicomUtility.GetValidImage(bitmap); int rows; int columns; byte[] pixels = DicomUtility.GetPixelsForDicom(bitmap, out rows, out columns); MemoryByteBuffer buffer = new MemoryByteBuffer(pixels); DicomDataset dataset = new DicomDataset(); Encoding encoding = Encoding.GetEncoding("GB18030"); dataset.Add <DicomUID>(DicomTag.SOPClassUID, new DicomUID[] { DicomUID.SecondaryCaptureImageStorage }); dataset.Add <DicomUID>(DicomTag.StudyInstanceUID, new DicomUID[] { DicomUtility.GenerateUid() }); dataset.Add <DicomUID>(DicomTag.SeriesInstanceUID, new DicomUID[] { DicomUtility.GenerateUid() }); dataset.Add <DicomUID>(DicomTag.SOPInstanceUID, new DicomUID[] { DicomUtility.GenerateUid() }); dataset.Add <string>(DicomTag.PatientID, new string[] { process_num }); dataset.Add(new DicomItem[] { DicomUtility.GetDicomItem(DicomTag.PatientName, encoding, patient_name) }); dataset.Add <string>(DicomTag.PatientBirthDate, new string[] { "00000000" }); dataset.Add(new DicomItem[] { DicomUtility.GetDicomItem(DicomTag.PatientAge, encoding, age) }); dataset.Add <string>(DicomTag.PatientSex, new string[] { sex }); if (studydate == "") { dataset.Add <DateTime>(DicomTag.StudyDate, new DateTime[] { DateTime.Now }); dataset.Add <DateTime>(DicomTag.StudyTime, new DateTime[] { DateTime.Now }); } else { dataset.Add <string>(DicomTag.StudyDate, new string[] { studydate }); dataset.Add <string>(DicomTag.StudyTime, new string[] { DateTime.Now.ToString("hhmmssfff") }); } dataset.Add <string>(DicomTag.AccessionNumber, new string[] { string.Empty }); dataset.Add <string>(DicomTag.ReferringPhysicianName, new string[] { string.Empty }); dataset.Add <string>(DicomTag.StudyID, new string[] { "1" }); dataset.Add <string>(DicomTag.SeriesNumber, new string[] { "1" }); dataset.Add <string>(DicomTag.ModalitiesInStudy, new string[] { modality }); dataset.Add <string>(DicomTag.Modality, new string[] { modality }); dataset.Add <string>(DicomTag.NumberOfStudyRelatedInstances, new string[] { "1" }); dataset.Add <string>(DicomTag.NumberOfStudyRelatedSeries, new string[] { "1" }); dataset.Add <string>(DicomTag.NumberOfSeriesRelatedInstances, new string[] { "1" }); dataset.Add <string>(DicomTag.PatientOrientation, new string[] { "F/A" }); dataset.Add <string>(DicomTag.ImageLaterality, new string[] { "U" }); dataset.Add(new DicomItem[] { DicomUtility.GetDicomItem(DicomTag.InstitutionName, encoding, hospital_name) }); dataset.Add <string>(DicomTag.StudyDescription, new string[] { study_description }); dataset.Add <string>(DicomTag.PhotometricInterpretation, new string[] { PhotometricInterpretation.Rgb.Value }); dataset.Add <ushort>(DicomTag.Rows, new ushort[] { (ushort)rows }); dataset.Add <ushort>(DicomTag.Columns, new ushort[] { (ushort)columns }); if (sop_uid != "") { dataset.Add <string>(DicomTag.SOPInstanceUID, new string[] { sop_uid }); } DicomPixelData pixelData = DicomPixelData.Create(dataset, true); pixelData.BitsStored = 8; pixelData.BitsAllocated = 8; pixelData.SamplesPerPixel = 3; pixelData.HighBit = 7; pixelData.PixelRepresentation = PixelRepresentation.Unsigned; pixelData.PlanarConfiguration = PlanarConfiguration.Interleaved; pixelData.AddFrame(buffer); DicomFile _dicomfile = new DicomFile(dataset); DicomFile file = new DicomFile(); switch (TransferSyntax) { case 0: file = _dicomfile.ChangeTransferSyntax(DicomTransferSyntax.JPEG2000Lossless, null); goto IL_579; case 1: file = _dicomfile.ChangeTransferSyntax(DicomTransferSyntax.RLELossless, null); goto IL_579; case 2: file = _dicomfile.ChangeTransferSyntax(DicomTransferSyntax.JPEGProcess14, null); goto IL_579; case 3: { int bits = _dicomfile.Dataset.Get <int>(DicomTag.BitsAllocated, 0, 8); DicomTransferSyntax syntax = DicomTransferSyntax.JPEGProcess1; if (bits == 16) { syntax = DicomTransferSyntax.JPEGProcess2_4; } file = _dicomfile.ChangeTransferSyntax(syntax, new DicomJpegParams { Quality = 100 }); goto IL_579; } case 4: file = _dicomfile.ChangeTransferSyntax(DicomTransferSyntax.ExplicitVRLittleEndian, null); goto IL_579; case 5: file = _dicomfile.ChangeTransferSyntax(DicomTransferSyntax.ExplicitVRBigEndian, null); goto IL_579; case 6: file = _dicomfile.ChangeTransferSyntax(DicomTransferSyntax.ImplicitVRLittleEndian, null); goto IL_579; case 8: file = _dicomfile.ChangeTransferSyntax(DicomTransferSyntax.ImplicitVRBigEndian, null); goto IL_579; } file = _dicomfile; IL_579: file.Save(dcmPath); }
/// <summary> /// 获取DICOM文件的字典数据 /// </summary> /// <param name="srcDcm"></param> /// <returns></returns> public static Dictionary <string, string> GetDcmMeta(string srcDcm) { Dictionary <string, string> dic = new Dictionary <string, string>(); DicomImage image = new DicomImage(srcDcm); IEnumerator <Dicom.DicomItem> iterator = image.Dataset.GetEnumerator(); while (iterator.MoveNext()) { if (iterator.Current is Dicom.DicomStringElement) { var item = iterator.Current as Dicom.DicomStringElement; if (!dic.ContainsKey(item.Tag.DictionaryEntry.Keyword)) { if (item.Tag.DictionaryEntry.Keyword == "WindowWidth") { dic.Add(item.Tag.DictionaryEntry.Keyword, image.WindowWidth.ToString()); } else if (item.Tag.DictionaryEntry.Keyword == "WindowCenter") { dic.Add(item.Tag.DictionaryEntry.Keyword, image.WindowCenter.ToString()); } else { var item_value = DicomUtility.GetStringFromDicomTag(image.Dataset, item.Tag); if (item_value == null) { if (item.Count > 0) { item_value = item.Get <string>(); } } dic.Add(item.Tag.DictionaryEntry.Keyword, item_value); } } } if (iterator.Current is Dicom.DicomSignedShort) { var item_short = iterator.Current as Dicom.DicomSignedShort; if (!dic.ContainsKey(item_short.Tag.DictionaryEntry.Keyword)) { if (item_short.Count > 0) { dic.Add(item_short.Tag.DictionaryEntry.Keyword, item_short.Get <string>()); } } } if (iterator.Current is Dicom.DicomUnsignedShort) { var item_short = iterator.Current as Dicom.DicomUnsignedShort; if (!dic.ContainsKey(item_short.Tag.DictionaryEntry.Keyword)) { if (item_short.Count > 0) { dic.Add(item_short.Tag.DictionaryEntry.Keyword, item_short.Get <string>()); } } } if (iterator.Current is Dicom.DicomUnsignedLong) { var item_short = iterator.Current as Dicom.DicomUnsignedLong; if (!dic.ContainsKey(item_short.Tag.DictionaryEntry.Keyword)) { if (item_short.Count > 0) { dic.Add(item_short.Tag.DictionaryEntry.Keyword, item_short.Get <string>()); } } } } return(dic); }
static void Main() { FleckLog.Level = LogLevel.Debug; var allSockets = new List <IWebSocketConnection>(); var server = new WebSocketServer("ws://0.0.0.0:8181"); server.Start(socket => { socket.OnOpen = () => { Console.WriteLine("Open!"); allSockets.Add(socket); }; socket.OnClose = () => { Console.WriteLine("Close!"); allSockets.Remove(socket); }; socket.OnMessage = message => { JavaScriptSerializer js = new JavaScriptSerializer(); ImgObj imgobj = js.Deserialize <ImgObj>(message); if (imgobj != null) { string Pic_Path = "R:\\a.png"; using (FileStream fs = new FileStream(Pic_Path, FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fs)) { byte[] data = Convert.FromBase64String(imgobj.ImageData); bw.Write(data); bw.Close(); } } DicomUtility.Bmp2Dcm(Pic_Path, Pic_Path.Replace("png", "dcm"), imgobj.Name, "F", imgobj.Age.ToString(), imgobj.ProcessNum, imgobj.Modality, imgobj.HospitalName, "US", DateTime.Now.ToString("yyyyMMdd")); string sopinstanceuid = DicomUtility.GetValueByTag(Pic_Path.Replace("png", "dcm"), Dicom.DicomTag.SOPInstanceUID); string patientid = DicomUtility.GetValueByTag(Pic_Path.Replace("png", "dcm"), Dicom.DicomTag.PatientID); try { DicomUtility.SendDcm(Pic_Path.Replace("png", "dcm"), "10.68.2.17", 104); //storescp.exe 104 -od "E:/dcms/" -fe ".dcm" -d string clientIp = socket.ConnectionInfo.ClientIpAddress; foreach (WebSocketConnection sock in allSockets.ToList()) { if (sock.ConnectionInfo.ClientIpAddress == clientIp) { sock.Send("E:/dicom_files/US/" + DateTime.Now.ToString("yyyyMMdd") + "/" + patientid + "/" + sopinstanceuid); } } } catch (Exception e) { socket.Send("上传图像失败,请检查网络设置" + e.Message); } } //Console.WriteLine("suc"); //allSockets.ToList().ForEach(s => s.Send("Echo: " + "suc")); }; }); var input = Console.ReadLine(); while (input != "exit") { foreach (var socket in allSockets.ToList()) { socket.Send(input); } input = Console.ReadLine(); } }