WadoQueryFrm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Xml;
  13. namespace PacsView3D
  14. {
  15. public partial class WadoQueryFrm : Form
  16. {
  17. int requestStudyCount = 0;
  18. List<study> studyList;
  19. string tmpPath = System.Environment.CurrentDirectory + "\\temp";
  20. string wado = string.Empty;
  21. string queryUrl = string.Empty;
  22. string downloadUrl = string.Empty;
  23. public WadoQueryFrm()
  24. {
  25. InitializeComponent();
  26. dataGridView1.Font = new Font("微软雅黑", 14);
  27. wado = ConfigurationManager.AppSettings["wado"];
  28. string user = ConfigurationManager.AppSettings["User"];
  29. string pwd = ConfigurationManager.AppSettings["Password"];
  30. wado = wado + "User=" + user + "&Password=" + pwd;
  31. queryUrl = wado + "&requestType=Query";
  32. downloadUrl = wado + "&requestType=WADO&contentType=application%2Fdicom&objectUID=";
  33. }
  34. private void btnQuery_Click(object sender, EventArgs e)
  35. {
  36. queryUrl = wado + "&requestType=Query";
  37. string whereString = "";
  38. dataGridView1.DataSource = null;
  39. if (txtPatientid.Text.Length>0)
  40. {
  41. whereString = whereString + "&patientID=" + txtPatientid.Text;
  42. }
  43. if (txtName.Text.Length > 0)
  44. {
  45. whereString = whereString + "&patientName=" + txtName.Text;
  46. }
  47. if (txtAccessNumber.Text.Length > 0)
  48. {
  49. whereString = whereString + "&accessionNumber=" + txtAccessNumber.Text;
  50. }
  51. if (txtModality.Text.Length > 0)
  52. {
  53. whereString = whereString + "&modality=" + txtModality.Text;
  54. }
  55. DateTime dt1 = this.datetimeStart.Value;
  56. string startData = dt1.ToString("yyyyMMdd");
  57. DateTime dt2 = this.datetimeEnd.Value;
  58. string endData = dt2.ToString("yyyyMMdd");
  59. whereString = whereString + "&studyDate=" + startData + "-" + endData;
  60. queryUrl = queryUrl + whereString;
  61. logtxt.AppendText(queryUrl);
  62. GetStudyList(queryUrl);
  63. try
  64. {
  65. this.dataGridView1.DataSource = studyList;
  66. toolStripStatusLabel1.Text = "Study:" + studyList.Count.ToString();
  67. }
  68. catch(Exception ex)
  69. {
  70. logtxt.AppendText(ex.Message.ToString());
  71. }
  72. }
  73. public void GetStudyList(string queryurl)
  74. {
  75. try
  76. {
  77. string studylistinfo = null;
  78. studylistinfo = HttpRequestHelper.HttpGetRequest(queryurl);
  79. logtxt.AppendText(studylistinfo);
  80. XmlDocument doc = new XmlDocument();
  81. if (studylistinfo != null)
  82. {
  83. doc.LoadXml(studylistinfo);
  84. }
  85. else
  86. {
  87. return;
  88. }
  89. //doc.Load("querystudylist.xml");//xml文件的路径
  90. XmlNodeList topM = doc.DocumentElement.ChildNodes;
  91. foreach (XmlElement element in topM)
  92. {
  93. if (element.Name.ToLower() == "dataset")
  94. {
  95. var aa = element.Attributes["studyRecCount"].Value;
  96. if (aa != null)
  97. {
  98. requestStudyCount = int.Parse(aa);
  99. }
  100. else
  101. {
  102. requestStudyCount = element.ChildNodes.Count;
  103. }
  104. studyList = new List<study>();
  105. List<string> objectuid = new List<string>();
  106. for (int i = 0; i < element.ChildNodes.Count; ++i)
  107. {
  108. study myStudy = new study();
  109. XmlElement studysEL = element.ChildNodes[i] as XmlElement;
  110. string seriesRecCount = studysEL.Attributes["seriesRecCount"].Value;
  111. myStudy.seriesCount = int.Parse(seriesRecCount);
  112. int studyImageCount = 0;
  113. foreach (XmlElement studyEL in studysEL)
  114. {
  115. switch (studyEL.Name.ToLower())
  116. {
  117. case "patientid":
  118. myStudy.patientID = studyEL.InnerText;
  119. break;
  120. case "patientname":
  121. myStudy.patientName = studyEL.InnerText;
  122. break;
  123. case "birthdate":
  124. myStudy.birthDate = studyEL.InnerText;
  125. break;
  126. case "sex":
  127. myStudy.sex = studyEL.InnerText;
  128. break;
  129. case "studydate":
  130. myStudy.studyDate = studyEL.InnerText;
  131. break;
  132. case "studytime":
  133. myStudy.studyTime = studyEL.InnerText;
  134. break;
  135. case "accessionnumber":
  136. myStudy.accessionNumber = studyEL.InnerText;
  137. break;
  138. case "studyuid":
  139. myStudy.studyUID = studyEL.InnerText;
  140. break;
  141. case "studydesc":
  142. myStudy.studyDesc = studyEL.InnerText;
  143. break;
  144. case "series":
  145. int seriesImageCount = int.Parse(studyEL.Attributes["imageRecCount"].Value);
  146. studyImageCount = studyImageCount + seriesImageCount;
  147. myStudy.imageCount = studyImageCount;
  148. XmlElement imagesEL = studyEL["image"];
  149. if (imagesEL != null)
  150. {
  151. foreach (XmlElement image in imagesEL)
  152. {
  153. if (image.Name.ToLower() == "objectuid")
  154. {
  155. if (image.InnerText.Length > 0)
  156. {
  157. objectuid.Add(image.InnerText);
  158. }
  159. }
  160. }
  161. }
  162. break;
  163. }
  164. }
  165. myStudy.objectUID = objectuid;
  166. studyList.Add(myStudy);
  167. }
  168. }
  169. }
  170. }
  171. catch(Exception ex)
  172. {
  173. logtxt.AppendText(ex.Message.ToString());
  174. }
  175. }
  176. private void WadoQueryFrm_Load(object sender, EventArgs e)
  177. {
  178. this.datetimeStart.Value = DateTime.Now;
  179. this.datetimeEnd.Value = DateTime.Now;
  180. }
  181. public string LoadStudy(string accessionNumber)
  182. {
  183. try
  184. {
  185. queryUrl = wado + "&requestType=Query&accessionNumber=" + accessionNumber;
  186. GetStudyList(queryUrl);
  187. study myStudy = studyList[0];
  188. string StudyPath = DownImage(myStudy);
  189. return StudyPath;
  190. }
  191. catch
  192. {
  193. return "";
  194. }
  195. }
  196. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  197. {
  198. int selectindex = dataGridView1.CurrentRow.Index;
  199. string data1 = dataGridView1.CurrentRow.Cells[1].Value.ToString();
  200. toolStripStatusLabel2.Text = data1;
  201. study selectedStudy = studyList[selectindex];
  202. }
  203. private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
  204. {
  205. try
  206. {
  207. int selectindex = dataGridView1.CurrentRow.Index;
  208. string data1 = dataGridView1.CurrentRow.Cells[1].Value.ToString();
  209. toolStripStatusLabel2.Text = data1;
  210. study selectedStudy = studyList[selectindex];
  211. toolStripStatusLabel3.Text = "准备下载图像:" + selectedStudy.objectUID.Count.ToString();
  212. toolStripProgressBar1.Maximum = selectedStudy.objectUID.Count +1;
  213. toolStripProgressBar1.Value = 0;
  214. string studyPath = null;
  215. studyPath = DownImage(selectedStudy);
  216. logtxt.AppendText("打开图像" + studyPath);
  217. MainFrm f1 = (MainFrm)this.Owner;
  218. f1.LoadStudy(studyPath);
  219. this.Owner.Show();
  220. this.Hide();
  221. }
  222. catch (Exception ex)
  223. {
  224. Console.WriteLine(ex);
  225. }
  226. }
  227. public string DownImage (study myStudy)
  228. {
  229. string studyPath = tmpPath + "\\" + myStudy.accessionNumber;
  230. if (Directory.Exists(studyPath) == false)
  231. {
  232. Directory.CreateDirectory(studyPath);
  233. }
  234. else
  235. {
  236. DeleteFolder(studyPath);
  237. Directory.CreateDirectory(studyPath);
  238. }
  239. foreach (string objectUID in myStudy.objectUID)
  240. {
  241. string downloadObjectURL = downloadUrl + objectUID;
  242. HttpDldFile df = new HttpDldFile();
  243. toolStripStatusLabel3.Text = "准备下载图像:" + myStudy.objectUID.Count.ToString() + "--" + toolStripProgressBar1.Value.ToString();
  244. bool dw = df.Download(downloadObjectURL, studyPath + "\\" + objectUID + ".dcm");
  245. if (dw)
  246. {
  247. Console.WriteLine("ok");
  248. toolStripProgressBar1.Value = toolStripProgressBar1.Value + 1;
  249. }
  250. }
  251. return studyPath;
  252. }
  253. private void DeleteFolder(string dir)
  254. {
  255. foreach (string d in Directory.GetFileSystemEntries(dir))
  256. {
  257. if (File.Exists(d))
  258. {
  259. try
  260. {
  261. FileInfo fi = new FileInfo(d);
  262. if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
  263. fi.Attributes = FileAttributes.Normal;
  264. File.Delete(d);//直接删除其中的文件
  265. }
  266. catch
  267. {
  268. }
  269. }
  270. else
  271. {
  272. try
  273. {
  274. DirectoryInfo d1 = new DirectoryInfo(d);
  275. if (d1.GetFiles().Length != 0)
  276. {
  277. DeleteFolder(d1.FullName);////递归删除子文件夹
  278. }
  279. Directory.Delete(d);
  280. }
  281. catch
  282. {
  283. }
  284. }
  285. }
  286. }
  287. private void statusStrip1_MouseDoubleClick(object sender, MouseEventArgs e)
  288. {
  289. logtxt.Left = 8;
  290. logtxt.Top = this.Height - logtxt.Height - 70;
  291. logtxt.Width = this.Width - 40;
  292. logtxt.Visible = !logtxt.Visible;
  293. }
  294. }
  295. }