OTSImageData.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.GetBSEImageSize();
  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 = m_MeasureApp.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. //去背景图
  151. public bool ShowRemoveBGImage(COTSSample WSample, int cWidth, int cHeight, byte[] cData, ref byte[] BSEImageNoBG)
  152. {
  153. try
  154. {
  155. var ImageFun = new OTSBSEImageFun();
  156. return ImageFun.GetBSEImage(WSample,cData, cHeight, cWidth, ref BSEImageNoBG);
  157. }
  158. catch (Exception ex)
  159. {
  160. return false;
  161. }
  162. }
  163. #region 获取工作距离与放大倍数
  164. /// <summary>
  165. /// 放大倍数
  166. /// </summary>
  167. public double GetMagnification()
  168. {
  169. //获取放大倍数
  170. return cfun.GetMagnification();
  171. }
  172. public void GetSemWorkingDistance(ref double a_dWorkingDistance)
  173. {
  174. //获取工作距离
  175. cfun.GetSemWorkingDistance(ref a_dWorkingDistance);
  176. }
  177. #endregion
  178. #region 获取SEM位置
  179. /// <summary>
  180. /// 获取SEM位置
  181. /// </summary>
  182. /// <returns></returns>
  183. public bool GetSemPositionXY(ref double a_dPositionX, ref double a_dPositionY, ref double a_dPositionR)
  184. {
  185. return cfun.GetSemPositionXY(ref a_dPositionX, ref a_dPositionY, ref a_dPositionR);
  186. }
  187. #endregion
  188. #region 电镜连接
  189. public bool ConnectionOTS()
  190. {
  191. if (cfun != null)
  192. {
  193. if (cfun.ConncetSem())
  194. {
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. #endregion
  201. #region 导出Excel 图片与数据
  202. /// <summary>
  203. /// 向Excel中插入图片与数据列表
  204. /// </summary>
  205. /// <param name="dtList"></param>
  206. /// <returns></returns>
  207. public bool InsertDataToExcelTable(List<System.Data.DataTable> a_list_ElementData,string a_filePath,List<string> a_ImgFilePath)
  208. {
  209. m_xe.Create();
  210. Microsoft.Office.Interop.Excel.Worksheet ws = m_xe.GetSheet("Sheet1");
  211. m_xe.m_ws = ws;
  212. string str_ExcelFilePath = a_filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
  213. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[0], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, 400, 300);
  214. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[1], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 320, 400, 100);
  215. if (str_ExcelFilePath.IndexOf(":") < 0)
  216. {
  217. return false;
  218. }
  219. if (m_xe.m_app == null)
  220. {
  221. return false;
  222. }
  223. //数据列表 行数
  224. int dataRow = 30;
  225. //写入标题
  226. foreach (var dgv in a_list_ElementData)
  227. {
  228. for (int i = 0; i < dgv.Columns.Count; i++)
  229. {
  230. m_xe.m_ws.Cells[dataRow+1, i + 1] = dgv.Columns[i].ColumnName;
  231. }
  232. //写入数值
  233. for (int r = 0; r < dgv.Rows.Count; r++)
  234. {
  235. for (int i = 0; i < dgv.Columns.Count; i++)
  236. {
  237. m_xe.m_ws.Cells[dataRow+r + 2, i + 1] = dgv.Rows[r].ItemArray[i];
  238. }
  239. System.Windows.Forms.Application.DoEvents();
  240. }
  241. }
  242. //列宽自适应
  243. m_xe.m_ws.Columns.EntireColumn.AutoFit();
  244. if (str_ExcelFilePath != "")
  245. {
  246. try
  247. {
  248. m_xe.m_wb.Saved = true;
  249. m_xe.m_wb.SaveCopyAs(str_ExcelFilePath);
  250. }
  251. catch (Exception ex)
  252. {
  253. log.Error("InsertDataToExcelTable错误日志: " + ex.ToString());
  254. }
  255. }
  256. //关闭excel
  257. m_xe.Close();
  258. //强行销毁
  259. GC.Collect();
  260. //干掉进程里的EXCEL
  261. foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
  262. {
  263. if (p.ProcessName == "EXCEL")
  264. {
  265. p.Kill();
  266. }
  267. }
  268. //删除图片文件
  269. foreach (string imgPath in a_ImgFilePath)
  270. {
  271. if (File.Exists(imgPath))
  272. {
  273. //如果存在则删除
  274. File.Delete(imgPath);
  275. }
  276. }
  277. System.Diagnostics.Process.Start(str_ExcelFilePath);
  278. return true;
  279. }
  280. #endregion
  281. }
  282. }