MainFrm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using Dicom;
  16. using Dicom.Log;
  17. using Dicom.Network;
  18. namespace PacsView3D
  19. {
  20. public partial class MainFrm : Form
  21. {
  22. private static string StoragePath = @".\DICOM";
  23. private static string calledAE = "";
  24. public static MainFrm mainfrm;
  25. private string encryptComputer = string.Empty;
  26. private bool isRegist = false;
  27. public static string studyPath = string.Empty;
  28. private string accessionnumber = string.Empty;
  29. string[] args = null;
  30. //private WadoQueryFrm queryFrm = new WadoQueryFrm();
  31. private QRFrm queryFrm = new QRFrm();
  32. //public MainFrm(string[] args)
  33. //{
  34. // InitializeComponent();
  35. // inifrm();
  36. // this.args = args;
  37. // if (args.Length > 0)
  38. // {
  39. // Console.WriteLine(string.Format("接收到了{0}个参数", args.Length));
  40. // foreach (var item in args)
  41. // {
  42. // if (item.Contains("path="))
  43. // {
  44. // studyPath = item.Replace("path=", "");
  45. // Console.WriteLine("path:{0}", studyPath);
  46. // }
  47. // if (item.Contains("accessionnumber="))
  48. // {
  49. // accessionnumber = item.Replace("accessionnumber=", "");
  50. // if(accessionnumber != string.Empty)
  51. // {
  52. // studyPath = queryFrm.LoadStudy(accessionnumber);
  53. // }
  54. // Console.WriteLine("studyuid:{0}", accessionnumber);
  55. // }
  56. // }
  57. // }
  58. // if (studyPath.Length>0)
  59. // {
  60. // this.axD3DA1.LoadDir(studyPath);
  61. // }
  62. //}
  63. public MainFrm()
  64. {
  65. InitializeComponent();
  66. Log4NetHelper.InitLog4Net(Application.StartupPath + "\\log4net.config");
  67. mainfrm = this;
  68. OpenSCP();
  69. inifrm();
  70. Log4NetHelper.WriteLog("程序启动");
  71. }
  72. private bool CheckRegist()
  73. {
  74. EncryptionHelper helper = new EncryptionHelper();
  75. string md5key = helper.GetMD5String(encryptComputer);
  76. Console.WriteLine(md5key);
  77. return CheckRegistData(md5key);
  78. }
  79. private bool CheckRegistData(string key)
  80. {
  81. if (RegistFileHelper.ExistRegistInfofile() == false)
  82. {
  83. isRegist = false;
  84. return false;
  85. }
  86. else
  87. {
  88. string info = RegistFileHelper.ReadRegistFile();
  89. var helper = new EncryptionHelper(EncryptionKeyEnum.KeyB);
  90. string registData = helper.DecryptString(info);
  91. if (key == registData)
  92. {
  93. isRegist = true;
  94. return true;
  95. }
  96. else
  97. {
  98. isRegist = false;
  99. return false;
  100. }
  101. }
  102. }
  103. public void inifrm()
  104. {
  105. string computer = ComputerInfo.GetComputerInfo();
  106. encryptComputer = new EncryptionHelper().EncryptString(computer);
  107. if (CheckRegist() == true)
  108. {
  109. this.Text = "PacsView3D --已注册";
  110. }
  111. else
  112. {
  113. this.Text = "PacsView3D --未注册:试用时间2019-5-30";
  114. RegistFileHelper.WriteComputerInfoFile(encryptComputer);
  115. DateTime lastDay = Convert.ToDateTime("2019-5-30");
  116. DateTime currentTime = System.DateTime.Now;
  117. if (currentTime > lastDay)
  118. {
  119. MessageBox.Show("版本试用已到期!");
  120. System.Diagnostics.Process tt = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);
  121. tt.Kill();
  122. }
  123. }
  124. axD3DA1.Initialize();
  125. this.panel1.Height = Convert.ToInt32( Height * 0.92);
  126. this.box2.Top = box1.Top + box1.Height + 5;
  127. this.box3.Top = box2.Top + box2.Height + 5;
  128. this.box2d.Top = box3.Top + box3.Height + 5;
  129. this.box3d.Top = box2.Top;
  130. this.box2d.Left = box1.Left;
  131. this.box2.Left = box1.Left;
  132. this.box3.Left = box1.Left;
  133. this.box3d.Left = box1.Left;
  134. this.box2d.Visible = false;
  135. this.box3d.Visible = true;
  136. this.box3d.Top = box2d.Top;
  137. this.box3d.Left = box2d.Left;
  138. this.axD3DA1.Show3dView();
  139. }
  140. private void btnload_Click(object sender, EventArgs e)
  141. {
  142. FolderBrowserDialog dialog = new FolderBrowserDialog();
  143. dialog.Description = "请选择文件路径";
  144. if (dialog.ShowDialog() == DialogResult.OK)
  145. {
  146. string fileoath = dialog.SelectedPath;
  147. this.axD3DA1.LoadDir(fileoath);
  148. }
  149. }
  150. public void LoadStudy (string path)
  151. {
  152. this.axD3DA1.Clear();
  153. this.axD3DA1.LoadDir(path);
  154. this.axD3DA1.Show3dView();
  155. }
  156. private void btn3D_Click(object sender, EventArgs e)
  157. {
  158. this.axD3DA1.Show3dView();
  159. box2d.Visible = false;
  160. box3d.Visible = true;
  161. box3d.Top = box2d.Top;
  162. box3d.Left = box2d.Left;
  163. }
  164. private void btn2D_Click(object sender, EventArgs e)
  165. {
  166. this.axD3DA1.Show2DView();
  167. box2d.Visible = true;
  168. box3d.Visible = false;
  169. }
  170. private void btmmpr_Click(object sender, EventArgs e)
  171. {
  172. this.axD3DA1.ShowMprView();
  173. box2d.Visible = false;
  174. box3d.Visible = false;
  175. }
  176. private void btnve_Click(object sender, EventArgs e)
  177. {
  178. this.axD3DA1.ShowVeView();
  179. box2d.Visible = false;
  180. box3d.Visible = false;
  181. }
  182. public void button1_Click(object sender, EventArgs e)
  183. {
  184. Button button = (Button)sender;
  185. if (button.Tag.ToString() == "1")
  186. {
  187. button.Tag = "0";
  188. this.axD3DA1.ShowPatientPanel(Convert.ToInt16(0));
  189. }
  190. else
  191. {
  192. this.axD3DA1.ShowPatientPanel(Convert.ToInt16(1));
  193. }
  194. }
  195. private void comboBoxSeries_SelectedIndexChanged(object sender, EventArgs e)
  196. {
  197. this.axD3DA1.Function2D(Convert.ToInt16(comboBoxSeries.SelectedIndex + 1));
  198. }
  199. private void comboBoxImage_SelectedIndexChanged(object sender, EventArgs e)
  200. {
  201. this.axD3DA1.Function2D(Convert.ToInt16(comboBoxImage.SelectedIndex + 6));
  202. }
  203. private void btnrule_Click(object sender, EventArgs e)
  204. {
  205. Button button = (Button)sender;
  206. this.axD3DA1.SetAnnTool(Convert.ToInt16(button.Tag));
  207. }
  208. private void Tools_Click(object sender, EventArgs e)
  209. {
  210. Button button = (Button)sender;
  211. this.axD3DA1.SetPublicTool(Convert.ToInt16(button.Tag));
  212. }
  213. private void button6_Click(object sender, EventArgs e)
  214. {
  215. Button button = (Button)sender;
  216. this.axD3DA1.Function2D(Convert.ToInt16(button.Tag));
  217. }
  218. private void btnPX_Click(object sender, EventArgs e)
  219. {
  220. this.axD3DA1.Function2D(Convert.ToInt16(17));
  221. }
  222. private void btnInfo_Click(object sender, EventArgs e)
  223. {
  224. Button button = (Button)sender;
  225. if (button.Tag.ToString() == "1")
  226. {
  227. button.Tag = "0";
  228. this.axD3DA1.ShowInformation(Convert.ToInt16(0));
  229. }
  230. else
  231. {
  232. this.axD3DA1.ShowInformation(Convert.ToInt16(1));
  233. }
  234. }
  235. private void btn3DTools_Click(object sender, EventArgs e)
  236. {
  237. Button button = (Button)sender;
  238. this.axD3DA1.Function3D(Convert.ToInt16(button.Tag));
  239. }
  240. private void btnList_Click(object sender, EventArgs e)
  241. {
  242. queryFrm.ShowDialog(this);
  243. }
  244. private void btnclear_Click(object sender, EventArgs e)
  245. {
  246. this.axD3DA1.Clear();
  247. }
  248. private void OpenSCP()
  249. {
  250. string port = ConfigurationManager.AppSettings["PORT"];
  251. calledAE = ConfigurationManager.AppSettings["QRAETITLE"];
  252. if (int.Parse(port)>0)
  253. {
  254. var server = DicomServer.Create<CStoreSCP>(int.Parse(port));
  255. Console.WriteLine("打开端口:" + port);
  256. }
  257. }
  258. private class CStoreSCP : DicomService, IDicomServiceProvider, IDicomCStoreProvider, IDicomCEchoProvider
  259. {
  260. private static readonly DicomTransferSyntax[] AcceptedTransferSyntaxes = new DicomTransferSyntax[]
  261. {
  262. DicomTransferSyntax.ExplicitVRLittleEndian,
  263. DicomTransferSyntax.ExplicitVRBigEndian,
  264. DicomTransferSyntax.ImplicitVRLittleEndian
  265. };
  266. private static readonly DicomTransferSyntax[] AcceptedImageTransferSyntaxes = new DicomTransferSyntax[]
  267. {
  268. // Lossless
  269. DicomTransferSyntax.JPEGLSLossless,
  270. DicomTransferSyntax.JPEG2000Lossless,
  271. DicomTransferSyntax.JPEGProcess14SV1,
  272. DicomTransferSyntax.JPEGProcess14,
  273. DicomTransferSyntax.RLELossless,
  274. // Lossy
  275. DicomTransferSyntax.JPEGLSNearLossless,
  276. DicomTransferSyntax.JPEG2000Lossy,
  277. DicomTransferSyntax.JPEGProcess1,
  278. DicomTransferSyntax.JPEGProcess2_4,
  279. // Uncompressed
  280. DicomTransferSyntax.ExplicitVRLittleEndian,
  281. DicomTransferSyntax.ExplicitVRBigEndian,
  282. DicomTransferSyntax.ImplicitVRLittleEndian
  283. };
  284. public CStoreSCP(INetworkStream stream, Encoding fallbackEncoding, Logger log)
  285. : base(stream, fallbackEncoding, log)
  286. {
  287. }
  288. public Task OnReceiveAssociationRequestAsync(DicomAssociation association)
  289. {
  290. Log4NetHelper.WriteLog("ReceiveAssociation.CalledAE:" + association.CalledAE);
  291. Log4NetHelper.WriteLog("ReceiveAssociation.CallingAE:" + association.CallingAE);
  292. //if (association.CalledAE != calledAE)
  293. //{
  294. // return SendAssociationRejectAsync(
  295. // DicomRejectResult.Permanent,
  296. // DicomRejectSource.ServiceUser,
  297. // DicomRejectReason.CalledAENotRecognized);
  298. //}
  299. foreach (var pc in association.PresentationContexts)
  300. {
  301. if (pc.AbstractSyntax == DicomUID.Verification) pc.AcceptTransferSyntaxes(AcceptedTransferSyntaxes);
  302. else if (pc.AbstractSyntax.StorageCategory != DicomStorageCategory.None) pc.AcceptTransferSyntaxes(AcceptedImageTransferSyntaxes);
  303. }
  304. Console.WriteLine("协商会话成功,SCU的AET为:" + association.CallingAE);
  305. return SendAssociationAcceptAsync(association);
  306. }
  307. public Task OnReceiveAssociationReleaseRequestAsync()
  308. {
  309. Console.WriteLine("成功完成了一次会话请求!study路径为:" + studyPath);
  310. mainfrm.LoadStudy(studyPath);
  311. return SendAssociationReleaseResponseAsync();
  312. }
  313. public void OnReceiveAbort(DicomAbortSource source, DicomAbortReason reason)
  314. {
  315. }
  316. public void OnConnectionClosed(Exception exception)
  317. {
  318. Console.WriteLine("连接关闭");
  319. }
  320. public DicomCStoreResponse OnCStoreRequest(DicomCStoreRequest request)
  321. {
  322. var studyUid = request.Dataset.GetSingleValue<string>(DicomTag.StudyInstanceUID);
  323. var instUid = request.SOPInstanceUID.UID;
  324. Console.WriteLine($"接收图像 {instUid}");
  325. var path = Path.GetFullPath(StoragePath);
  326. studyPath = Path.Combine(path, studyUid);
  327. path = Path.Combine(path, studyUid);
  328. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  329. path = Path.Combine(path, instUid) + ".dcm";
  330. request.File.Save(path);
  331. return new DicomCStoreResponse(request, DicomStatus.Success);
  332. }
  333. public void OnCStoreRequestException(string tempFileName, Exception e)
  334. {
  335. Console.WriteLine("接收产生异常!");
  336. // let library handle logging and error response
  337. }
  338. public DicomCEchoResponse OnCEchoRequest(DicomCEchoRequest request)
  339. {
  340. Console.WriteLine("接收到ECHO请求!");
  341. return new DicomCEchoResponse(request, DicomStatus.Success);
  342. }
  343. }
  344. }
  345. }