OTSImageData.cs 10 KB

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