DicomHelper.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Dicom;
  2. using Dicom.Network;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Vinno.IUS.Common.Log;
  7. using static Dicom.Network.DicomCEchoRequest;
  8. using DicomClient = Dicom.Network.Client.DicomClient;
  9. namespace Vinno.FIS.Sonopost.Helpers
  10. {
  11. internal class DicomHelper
  12. {
  13. /// <summary>
  14. /// save dicom to file
  15. /// </summary>
  16. /// <param name="fileName"></param>
  17. /// <param name="dicomFile"></param>
  18. public static void Save(string fileName, DicomFile dicomFile)
  19. {
  20. using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 65536, FileOptions.WriteThrough))
  21. {
  22. dicomFile.Save(fs);
  23. fs.Flush();
  24. }
  25. }
  26. public static async Task<bool> TestDicomAsync(string address, int port, string localAeTitle, string targetAeTitle)
  27. {
  28. try
  29. {
  30. bool success = false;
  31. var resetEvent = new ManualResetEvent(false);
  32. var dicomClient = new DicomClient(address, port, false, localAeTitle, targetAeTitle);
  33. var echo = new DicomCEchoRequest();
  34. ResponseDelegate reponse = (DicomCEchoRequest req, DicomCEchoResponse response) =>
  35. {
  36. success = response.Status == DicomStatus.Success;
  37. Logger.WriteLineInfo($"Test DicomAsync Status :{response.Status}");
  38. resetEvent.Set();
  39. };
  40. echo.OnResponseReceived += reponse;
  41. await dicomClient.AddRequestAsync(echo);
  42. Task.Run(() =>
  43. {
  44. try
  45. {
  46. dicomClient.SendAsync();
  47. }
  48. catch
  49. {
  50. //ignore
  51. }
  52. });
  53. resetEvent.WaitOne(5000);
  54. echo.OnResponseReceived -= reponse;
  55. return success;
  56. }
  57. catch
  58. {
  59. //ignore
  60. }
  61. return false;
  62. }
  63. }
  64. }