AivfoHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Drawing.Imaging;
  8. using System.Diagnostics;
  9. namespace ivf_tl_UtilHelper
  10. {
  11. public class AivfoHelper
  12. {
  13. /// <summary>
  14. /// 抠图,0表示抠图失败
  15. /// </summary>
  16. /// <param name="imgData">图片字节流,读图片的话会有54字节的图片头</param>
  17. /// <param name="width">宽度</param>
  18. /// <param name="height">高度</param>
  19. /// <param name="path">抠出来的图片地址</param>
  20. /// <param name="sourcePath">保存源图的地址</param>
  21. /// <param name="isSave">1表示强制保存源图,0表示不强制保存源图</param>
  22. /// <returns></returns>
  23. [DllImport(@"DependFile\newccd\Project2.dll", EntryPoint = "#1", CallingConvention = CallingConvention.Cdecl)]
  24. public static extern IntPtr ImageProcessing(byte[] imgData, int width, int height, string path, string sourcePath, int isSave, string wellname, string devTime, int letoffset, int bottomOffset);
  25. /// <summary>
  26. /// 获取图片分数并保存图片,图片上有当前位置,以及分数
  27. /// </summary>
  28. /// <param name="bitmapData">图片字节流,读图片的话会有54字节的图片头</param>
  29. /// <param name="w">宽度</param>
  30. /// <param name="h">高度</param>
  31. /// <returns></returns>
  32. [DllImport(@"DependFile\newccd\Project2.dll", EntryPoint = "#5", CallingConvention = CallingConvention.Cdecl)]
  33. public extern static double GetImageScoreAndSaveImage(byte[] bitmapData, int w, int h, string dir, int id, string dir1, int letoffset, int bottomOffset);
  34. [DllImport(@"DependFile\newccd\Project2.dll", EntryPoint = "#6", CallingConvention = CallingConvention.Cdecl)]
  35. public extern static int Save1(string fileName, string wellName, string fayuString);
  36. /// <summary>
  37. /// 1是成功 0失败
  38. /// </summary>
  39. /// <param name="bitmapData"></param>
  40. /// <param name="width"></param>
  41. /// <param name="height"></param>
  42. /// <param name="path"></param>
  43. [DllImport(@"DependFile\newccd\Project2.dll", EntryPoint = "#7", CallingConvention = CallingConvention.Cdecl)]
  44. public extern static int Save2(byte[] bitmapData, int width, int height, string path);
  45. public static byte[] GetImageData(string fileName)
  46. {
  47. try
  48. {
  49. if (!File.Exists(fileName))
  50. {
  51. return null;
  52. }
  53. ImageFormat imageFormat = ImageFormat.Jpeg;
  54. switch (Path.GetExtension(fileName).ToLower())
  55. {
  56. case ".jpg":
  57. imageFormat = ImageFormat.Jpeg;
  58. break;
  59. case ".bmp":
  60. imageFormat = ImageFormat.Bmp;
  61. break;
  62. case ".png":
  63. imageFormat = ImageFormat.Png;
  64. break;
  65. default:
  66. break;
  67. }
  68. System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fileName);
  69. MemoryStream memoryStream = new MemoryStream();
  70. bitmap.Save(memoryStream, imageFormat);
  71. var a = memoryStream.ToArray();
  72. memoryStream.Close();
  73. memoryStream.Dispose();
  74. bitmap.Dispose();
  75. return a;
  76. }
  77. catch (Exception ex)
  78. {
  79. return null;
  80. }
  81. }
  82. public static byte[] GetImageData1(string fileName)
  83. {
  84. try
  85. {
  86. if (!File.Exists(fileName))
  87. {
  88. return null;
  89. }
  90. using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  91. {
  92. byte[] imgData = new byte[fileStream.Length];
  93. fileStream.Read(imgData, 0, imgData.Length);
  94. fileStream.Close();
  95. fileStream.Dispose();
  96. return imgData;
  97. }
  98. }
  99. catch (Exception)
  100. {
  101. throw;
  102. }
  103. }
  104. }
  105. }