using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; namespace PacsView3D { public partial class WadoQueryFrm : Form { int requestStudyCount = 0; List studyList; string tmpPath = System.Environment.CurrentDirectory + "\\temp"; string wado = string.Empty; string queryUrl = string.Empty; string downloadUrl = string.Empty; public WadoQueryFrm() { InitializeComponent(); dataGridView1.Font = new Font("微软雅黑", 14); wado = ConfigurationManager.AppSettings["wado"]; string user = ConfigurationManager.AppSettings["User"]; string pwd = ConfigurationManager.AppSettings["Password"]; wado = wado + "User=" + user + "&Password=" + pwd; queryUrl = wado + "&requestType=Query"; downloadUrl = wado + "&requestType=WADO&contentType=application%2Fdicom&objectUID="; } private void btnQuery_Click(object sender, EventArgs e) { queryUrl = wado + "&requestType=Query"; string whereString = ""; dataGridView1.DataSource = null; if (txtPatientid.Text.Length>0) { whereString = whereString + "&patientID=" + txtPatientid.Text; } if (txtName.Text.Length > 0) { whereString = whereString + "&patientName=" + txtName.Text; } if (txtAccessNumber.Text.Length > 0) { whereString = whereString + "&accessionNumber=" + txtAccessNumber.Text; } if (txtModality.Text.Length > 0) { whereString = whereString + "&modality=" + txtModality.Text; } DateTime dt1 = this.datetimeStart.Value; string startData = dt1.ToString("yyyyMMdd"); DateTime dt2 = this.datetimeEnd.Value; string endData = dt2.ToString("yyyyMMdd"); whereString = whereString + "&studyDate=" + startData + "-" + endData; queryUrl = queryUrl + whereString; logtxt.AppendText(queryUrl); GetStudyList(queryUrl); try { this.dataGridView1.DataSource = studyList; toolStripStatusLabel1.Text = "Study:" + studyList.Count.ToString(); } catch(Exception ex) { logtxt.AppendText(ex.Message.ToString()); } } public void GetStudyList(string queryurl) { try { string studylistinfo = null; studylistinfo = HttpRequestHelper.HttpGetRequest(queryurl); logtxt.AppendText(studylistinfo); XmlDocument doc = new XmlDocument(); if (studylistinfo != null) { doc.LoadXml(studylistinfo); } else { return; } //doc.Load("querystudylist.xml");//xml文件的路径 XmlNodeList topM = doc.DocumentElement.ChildNodes; foreach (XmlElement element in topM) { if (element.Name.ToLower() == "dataset") { var aa = element.Attributes["studyRecCount"].Value; if (aa != null) { requestStudyCount = int.Parse(aa); } else { requestStudyCount = element.ChildNodes.Count; } studyList = new List(); List objectuid = new List(); for (int i = 0; i < element.ChildNodes.Count; ++i) { study myStudy = new study(); XmlElement studysEL = element.ChildNodes[i] as XmlElement; string seriesRecCount = studysEL.Attributes["seriesRecCount"].Value; myStudy.seriesCount = int.Parse(seriesRecCount); int studyImageCount = 0; foreach (XmlElement studyEL in studysEL) { switch (studyEL.Name.ToLower()) { case "patientid": myStudy.patientID = studyEL.InnerText; break; case "patientname": myStudy.patientName = studyEL.InnerText; break; case "birthdate": myStudy.birthDate = studyEL.InnerText; break; case "sex": myStudy.sex = studyEL.InnerText; break; case "studydate": myStudy.studyDate = studyEL.InnerText; break; case "studytime": myStudy.studyTime = studyEL.InnerText; break; case "accessionnumber": myStudy.accessionNumber = studyEL.InnerText; break; case "studyuid": myStudy.studyUID = studyEL.InnerText; break; case "studydesc": myStudy.studyDesc = studyEL.InnerText; break; case "series": int seriesImageCount = int.Parse(studyEL.Attributes["imageRecCount"].Value); studyImageCount = studyImageCount + seriesImageCount; myStudy.imageCount = studyImageCount; XmlElement imagesEL = studyEL["image"]; if (imagesEL != null) { foreach (XmlElement image in imagesEL) { if (image.Name.ToLower() == "objectuid") { if (image.InnerText.Length > 0) { objectuid.Add(image.InnerText); } } } } break; } } myStudy.objectUID = objectuid; studyList.Add(myStudy); } } } } catch(Exception ex) { logtxt.AppendText(ex.Message.ToString()); } } private void WadoQueryFrm_Load(object sender, EventArgs e) { this.datetimeStart.Value = DateTime.Now; this.datetimeEnd.Value = DateTime.Now; } public string LoadStudy(string accessionNumber) { try { queryUrl = wado + "&requestType=Query&accessionNumber=" + accessionNumber; GetStudyList(queryUrl); study myStudy = studyList[0]; string StudyPath = DownImage(myStudy); return StudyPath; } catch { return ""; } } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { int selectindex = dataGridView1.CurrentRow.Index; string data1 = dataGridView1.CurrentRow.Cells[1].Value.ToString(); toolStripStatusLabel2.Text = data1; study selectedStudy = studyList[selectindex]; } private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) { try { int selectindex = dataGridView1.CurrentRow.Index; string data1 = dataGridView1.CurrentRow.Cells[1].Value.ToString(); toolStripStatusLabel2.Text = data1; study selectedStudy = studyList[selectindex]; toolStripStatusLabel3.Text = "准备下载图像:" + selectedStudy.objectUID.Count.ToString(); toolStripProgressBar1.Maximum = selectedStudy.objectUID.Count +1; toolStripProgressBar1.Value = 0; string studyPath = null; studyPath = DownImage(selectedStudy); logtxt.AppendText("打开图像" + studyPath); MainFrm f1 = (MainFrm)this.Owner; f1.LoadStudy(studyPath); this.Owner.Show(); this.Hide(); } catch (Exception ex) { Console.WriteLine(ex); } } public string DownImage (study myStudy) { string studyPath = tmpPath + "\\" + myStudy.accessionNumber; if (Directory.Exists(studyPath) == false) { Directory.CreateDirectory(studyPath); } else { DeleteFolder(studyPath); Directory.CreateDirectory(studyPath); } foreach (string objectUID in myStudy.objectUID) { string downloadObjectURL = downloadUrl + objectUID; HttpDldFile df = new HttpDldFile(); toolStripStatusLabel3.Text = "准备下载图像:" + myStudy.objectUID.Count.ToString() + "--" + toolStripProgressBar1.Value.ToString(); bool dw = df.Download(downloadObjectURL, studyPath + "\\" + objectUID + ".dcm"); if (dw) { Console.WriteLine("ok"); toolStripProgressBar1.Value = toolStripProgressBar1.Value + 1; } } return studyPath; } private void DeleteFolder(string dir) { foreach (string d in Directory.GetFileSystemEntries(dir)) { if (File.Exists(d)) { try { FileInfo fi = new FileInfo(d); if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1) fi.Attributes = FileAttributes.Normal; File.Delete(d);//直接删除其中的文件 } catch { } } else { try { DirectoryInfo d1 = new DirectoryInfo(d); if (d1.GetFiles().Length != 0) { DeleteFolder(d1.FullName);////递归删除子文件夹 } Directory.Delete(d); } catch { } } } } private void statusStrip1_MouseDoubleClick(object sender, MouseEventArgs e) { logtxt.Left = 8; logtxt.Top = this.Height - logtxt.Height - 70; logtxt.Width = this.Width - 40; logtxt.Visible = !logtxt.Visible; } } }