OTSImageData.cs 9.5 KB

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