OTSImageData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. /*这个引用包含作图方法*/
  3. /*这个引用包含作图的Bitmap*/
  4. using System.Drawing;
  5. using System.Collections.Generic;
  6. using OTSCLRINTERFACE;
  7. using System.Data;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using OTSDataType;
  11. namespace OTSMeasureApp
  12. {
  13. public enum idLine
  14. {
  15. //曲线
  16. GrayLine = 0,
  17. //灰度图
  18. GrayImage = 1
  19. }
  20. class OTSImageData
  21. {
  22. COTSControlFunExport cfun = null;
  23. OTSMeasureStatusWindow m_MeasureStatuWindow = null;
  24. OTSIncAMeasureAppForm m_MeasureApp = null;
  25. //当前默认值
  26. public idLine Line = idLine.GrayImage;
  27. //String SEMName = "";
  28. int ImageValue = 0;
  29. static ExcelEdit m_xe = new ExcelEdit();
  30. NLog.Logger log ;
  31. public OTSImageData(OTSMeasureStatusWindow MeasureStatuWindow, OTSIncAMeasureAppForm oTSIncAMeasureAppForm)
  32. {
  33. log = NLog.LogManager.GetCurrentClassLogger();
  34. m_MeasureStatuWindow = MeasureStatuWindow;
  35. m_MeasureApp = oTSIncAMeasureAppForm;
  36. }
  37. /// <summary>
  38. /// 计算调试图灰度分布数据
  39. /// </summary>
  40. /// <param name="Imagedata"></param>
  41. /// <param name="GrayLevelData"></param>
  42. //Imagedata 是图像 GrayLevelData是Y轴的数
  43. public double[] GetGaryData(byte[] Imagedata, double[] GrayLevelData)
  44. {
  45. try
  46. {
  47. //获得下标每一点的灰度值,并在数组里加一
  48. for (int i = 0; i < Imagedata.Length; i++)
  49. {
  50. byte graylevel = Imagedata[i];
  51. if (graylevel > 0 && graylevel < 255)
  52. {
  53. GrayLevelData[graylevel] += 1;
  54. }
  55. }
  56. return GrayLevelData;
  57. }
  58. catch (Exception ex)
  59. {
  60. log.Error("(OTSMeasureStatusWindow.GetGaryData) " + ex.ToString());
  61. return null;
  62. }
  63. }
  64. public static double[] GetStaticGaryData(byte[] Imagedata, double[] GrayLevelData)
  65. {
  66. try
  67. {
  68. //获得下标每一点的灰度值,并在数组里加一
  69. for (int i = 0; i < Imagedata.Length; i++)
  70. {
  71. byte graylevel = Imagedata[i];
  72. if (graylevel > 0 && graylevel < 255)
  73. {
  74. GrayLevelData[graylevel] += 1;
  75. }
  76. }
  77. return GrayLevelData;
  78. }
  79. catch (Exception)
  80. {
  81. return null;
  82. }
  83. }
  84. /// <summary>
  85. /// 计算去背景BSE图灰度分布数据
  86. /// </summary>
  87. /// <param name="Imagedata"></param>
  88. /// <param name="GrayAbandonLevelData"></param>
  89. //Imagedata 是图像 GrayLevelData是Y轴的数
  90. public double[] GetGrayAbandonData(byte[] Imagedata, double[] GrayAbandonLevelData)
  91. {
  92. try
  93. {
  94. //获得下标每一点的灰度值,并在数组里加一
  95. for (int i = 0; i < Imagedata.Length; i++)
  96. {
  97. byte graylevel = Imagedata[i];
  98. if (graylevel > 0 && graylevel < 255)
  99. {
  100. GrayAbandonLevelData[graylevel] += 1;
  101. }
  102. }
  103. return GrayAbandonLevelData;
  104. }
  105. catch (Exception ex)
  106. {
  107. log.Error("(OTSMeasureStatusWindow.GetGaryData) " + ex.ToString());
  108. return null;
  109. }
  110. }
  111. /// <summary>
  112. /// 获取调试图
  113. /// </summary>
  114. /// <param name="iWidth"></param>
  115. /// <param name="iHeight"></param>
  116. /// <param name="bData"></param>
  117. /// <returns></returns>
  118. public bool GetImagData(int iWidth, int iHeight, ref byte[] bData)
  119. {
  120. try
  121. {
  122. //获取图像数据
  123. int resultCount = iHeight * iWidth;
  124. //获取电镜中图像大小
  125. string str = m_MeasureApp.m_ProjParam.GetBSEImageResolution();
  126. string[] sArray = str.Split('X');
  127. if (sArray[0] != "" && sArray[1] != "")
  128. {
  129. iWidth = Convert.ToInt32(sArray[0]);
  130. iHeight = Convert.ToInt32(sArray[1]);
  131. }
  132. //设置获取图像的Byte数组
  133. bData = new byte[iWidth * iHeight];
  134. ImageValue = GetImageData(iWidth, iHeight, ref bData, true);
  135. if (resultCount == ImageValue)
  136. {
  137. return true;
  138. }
  139. else
  140. {
  141. return false;
  142. }
  143. }
  144. catch (Exception)
  145. {
  146. //记录日志
  147. return false;
  148. }
  149. }
  150. public int GetImageData(int iWidth, int iHeight, ref byte[] ImageData, bool bSimulatFlag = false)
  151. {
  152. //获取BSE图片数据
  153. OTSBSEImageFun m_GetBseImage = new OTSBSEImageFun();
  154. return m_GetBseImage.GetScanImage(iWidth, iHeight, ref ImageData);
  155. }
  156. //去背景图
  157. public bool GetRemoveBGImage(COTSImageProcParam ImgProcPrm, int cWidth, int cHeight, byte[] cData, ref byte[] BSEImageNoBG)
  158. {
  159. try
  160. {
  161. var ImageFun = new OTSBSEImageFun();
  162. return ImageFun.GetBSEImage(ImgProcPrm, cData, cHeight, cWidth, ref BSEImageNoBG);
  163. }
  164. catch (Exception ex)
  165. {
  166. return false;
  167. }
  168. }
  169. public bool GetSpecialGrayImage(byte[] cData, int cWidth, int cHeight, int startGray,int endGray, ref byte[] BSEImageNoBG)
  170. {
  171. try
  172. {
  173. var ImageFun = new OTSBSEImageFun();
  174. return ImageFun.GetBSEImage(cData, cHeight, cWidth,startGray,endGray, ref BSEImageNoBG);
  175. }
  176. catch (Exception ex)
  177. {
  178. return false;
  179. }
  180. }
  181. #region 获取工作距离与放大倍数
  182. /// <summary>
  183. /// 放大倍数
  184. /// </summary>
  185. public double GetMagnification()
  186. {
  187. //获取放大倍数
  188. return cfun.GetMagnification();
  189. }
  190. public void GetSemWorkingDistance(ref double a_dWorkingDistance)
  191. {
  192. //获取工作距离
  193. cfun.GetSemWorkingDistance(ref a_dWorkingDistance);
  194. }
  195. #endregion
  196. #region 获取SEM位置
  197. /// <summary>
  198. /// 获取SEM位置
  199. /// </summary>
  200. /// <returns></returns>
  201. public bool GetSemPositionXY(ref double a_dPositionX, ref double a_dPositionY, ref double a_dPositionR)
  202. {
  203. return cfun.GetSemPositionXY(ref a_dPositionX, ref a_dPositionY, ref a_dPositionR);
  204. }
  205. #endregion
  206. #region 电镜连接
  207. public bool ConnectionOTS()
  208. {
  209. if (cfun != null)
  210. {
  211. if (cfun.ConncetSem())
  212. {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. #endregion
  219. #region 导出Excel 图片与数据
  220. /// <summary>
  221. /// 向Excel中插入图片与数据列表
  222. /// </summary>
  223. /// <param name="dtList"></param>
  224. /// <returns></returns>
  225. public bool InsertDataToExcelTable(List<System.Data.DataTable> a_list_ElementData,string a_filePath,List<string> a_ImgFilePath)
  226. {
  227. m_xe.Create();
  228. Microsoft.Office.Interop.Excel.Worksheet ws = m_xe.GetSheet("Sheet1");
  229. m_xe.m_ws = ws;
  230. string str_ExcelFilePath = a_filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
  231. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[0], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, 400, 300);
  232. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[1], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 320, 400, 100);
  233. if (str_ExcelFilePath.IndexOf(":") < 0)
  234. {
  235. return false;
  236. }
  237. if (m_xe.m_app == null)
  238. {
  239. return false;
  240. }
  241. //数据列表 行数
  242. int dataRow = 30;
  243. //写入标题
  244. foreach (var dgv in a_list_ElementData)
  245. {
  246. for (int i = 0; i < dgv.Columns.Count; i++)
  247. {
  248. m_xe.m_ws.Cells[dataRow+1, i + 1] = dgv.Columns[i].ColumnName;
  249. }
  250. //写入数值
  251. for (int r = 0; r < dgv.Rows.Count; r++)
  252. {
  253. for (int i = 0; i < dgv.Columns.Count; i++)
  254. {
  255. m_xe.m_ws.Cells[dataRow+r + 2, i + 1] = dgv.Rows[r].ItemArray[i];
  256. }
  257. System.Windows.Forms.Application.DoEvents();
  258. }
  259. }
  260. //列宽自适应
  261. m_xe.m_ws.Columns.EntireColumn.AutoFit();
  262. if (str_ExcelFilePath != "")
  263. {
  264. try
  265. {
  266. m_xe.m_wb.Saved = true;
  267. m_xe.m_wb.SaveCopyAs(str_ExcelFilePath);
  268. }
  269. catch (Exception ex)
  270. {
  271. log.Error("InsertDataToExcelTable错误日志: " + ex.ToString());
  272. }
  273. }
  274. //关闭excel
  275. m_xe.Close();
  276. //强行销毁
  277. GC.Collect();
  278. //干掉进程里的EXCEL
  279. foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
  280. {
  281. if (p.ProcessName == "EXCEL")
  282. {
  283. p.Kill();
  284. }
  285. }
  286. //删除图片文件
  287. foreach (string imgPath in a_ImgFilePath)
  288. {
  289. if (File.Exists(imgPath))
  290. {
  291. //如果存在则删除
  292. File.Delete(imgPath);
  293. }
  294. }
  295. System.Diagnostics.Process.Start(str_ExcelFilePath);
  296. return true;
  297. }
  298. #endregion
  299. }
  300. }