123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- using System;
- /*这个引用包含作图方法*/
- /*这个引用包含作图的Bitmap*/
- using System.Drawing;
- using System.Collections.Generic;
- using OTSCLRINTERFACE;
- using System.Data;
- using System.Windows.Forms;
- using System.IO;
- using OTSDataType;
- namespace OTSMeasureApp
- {
- public enum idLine
- {
- //曲线
- GrayLine = 0,
- //灰度图
- GrayImage = 1
- }
- class OTSImageData
- {
- COTSControlFunExport cfun = null;
- OTSMeasureStatusWindow m_MeasureStatuWindow = null;
- OTSIncAMeasureAppForm m_MeasureApp = null;
-
- //当前默认值
- public idLine Line = idLine.GrayImage;
- //String SEMName = "";
- int ImageValue = 0;
- static ExcelEdit m_xe /*= new ExcelEdit()*/;
- NLog.Logger log ;
- public OTSImageData(OTSMeasureStatusWindow MeasureStatuWindow, OTSIncAMeasureAppForm oTSIncAMeasureAppForm)
- {
- log = NLog.LogManager.GetCurrentClassLogger();
-
- m_MeasureStatuWindow = MeasureStatuWindow;
- m_MeasureApp = oTSIncAMeasureAppForm;
- m_xe = new ExcelEdit();
- }
- /// <summary>
- /// 计算调试图灰度分布数据
- /// </summary>
- /// <param name="Imagedata"></param>
- /// <param name="GrayLevelData"></param>
- //Imagedata 是图像 GrayLevelData是Y轴的数
- public double[] GetGaryData(byte[] Imagedata, double[] GrayLevelData)
- {
- try
- {
- //获得下标每一点的灰度值,并在数组里加一
- for (int i = 0; i < Imagedata.Length; i++)
- {
- byte graylevel = Imagedata[i];
- if (graylevel > 0 && graylevel < 255)
- {
- GrayLevelData[graylevel] += 1;
- }
- }
- return GrayLevelData;
- }
- catch (Exception ex)
- {
- log.Error("(OTSMeasureStatusWindow.GetGaryData) " + ex.ToString());
- return null;
- }
- }
- public static double[] GetStaticGaryData(byte[] Imagedata, double[] GrayLevelData)
- {
- try
- {
- //获得下标每一点的灰度值,并在数组里加一
- for (int i = 0; i < Imagedata.Length; i++)
- {
- byte graylevel = Imagedata[i];
- if (graylevel > 0 && graylevel < 255)
- {
- GrayLevelData[graylevel] += 1;
- }
- }
- return GrayLevelData;
- }
- catch (Exception)
- {
- return null;
- }
- }
- /// <summary>
- /// 计算去背景BSE图灰度分布数据
- /// </summary>
- /// <param name="Imagedata"></param>
- /// <param name="GrayAbandonLevelData"></param>
- //Imagedata 是图像 GrayLevelData是Y轴的数
- public double[] GetGrayAbandonData(byte[] Imagedata, double[] GrayAbandonLevelData)
- {
- try
- {
- //获得下标每一点的灰度值,并在数组里加一
- for (int i = 0; i < Imagedata.Length; i++)
- {
- byte graylevel = Imagedata[i];
- if (graylevel > 0 && graylevel < 255)
- {
- GrayAbandonLevelData[graylevel] += 1;
- }
- }
- return GrayAbandonLevelData;
- }
- catch (Exception ex)
- {
- log.Error("(OTSMeasureStatusWindow.GetGaryData) " + ex.ToString());
- return null;
- }
- }
- /// <summary>
- /// 获取调试图
- /// </summary>
- /// <param name="iWidth"></param>
- /// <param name="iHeight"></param>
- /// <param name="bData"></param>
- /// <returns></returns>
- public bool GetImagData(int iWidth, int iHeight, ref byte[] bData)
- {
- try
- {
- //获取图像数据
- int resultCount = iHeight * iWidth;
- //获取电镜中图像大小
- string str = m_MeasureApp.m_ProjParam.GetBSEImageResolution();
- string[] sArray = str.Split('X');
- if (sArray[0] != "" && sArray[1] != "")
- {
- iWidth = Convert.ToInt32(sArray[0]);
- iHeight = Convert.ToInt32(sArray[1]);
- }
- //设置获取图像的Byte数组
- bData = new byte[iWidth * iHeight];
- ImageValue = GetImageData(iWidth, iHeight, ref bData, true);
- if (resultCount == ImageValue)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (Exception)
- {
- //记录日志
- return false;
- }
- }
- public int GetImageData(int iWidth, int iHeight, ref byte[] ImageData, bool bSimulatFlag = false)
- {
- //获取BSE图片数据
- OTSBSEImageFun m_GetBseImage = new OTSBSEImageFun();
- return m_GetBseImage.GetScanImage(iWidth, iHeight, ref ImageData);
- }
- //去背景图
- public bool GetRemoveBGImage(COTSImageProcParam ImgProcPrm, double pixelSize,int cWidth, int cHeight, byte[] cData, ref byte[] BSEImageNoBG)
- {
- try
- {
- var ImageFun = new OTSBSEImageFun();
- return ImageFun.GetBSEImage(ImgProcPrm,pixelSize, cData, cHeight, cWidth, ref BSEImageNoBG);
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- public bool GetSpecialGrayImage(byte[] cData, int cWidth, int cHeight, int startGray,int endGray, ref byte[] BSEImageNoBG)
- {
- try
- {
- var ImageFun = new OTSBSEImageFun();
- return ImageFun.GetBSEImage(cData, cHeight, cWidth,startGray,endGray, ref BSEImageNoBG);
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- #region 获取工作距离与放大倍数
- /// <summary>
- /// 放大倍数
- /// </summary>
- public double GetMagnification()
- {
- //获取放大倍数
- return cfun.GetMagnification();
- }
- public void GetSemWorkingDistance(ref double a_dWorkingDistance)
- {
- //获取工作距离
- cfun.GetSemWorkingDistance(ref a_dWorkingDistance);
- }
- #endregion
- #region 获取SEM位置
- /// <summary>
- /// 获取SEM位置
- /// </summary>
- /// <returns></returns>
- public bool GetSemPositionXY(ref double a_dPositionX, ref double a_dPositionY, ref double a_dPositionR)
- {
- return cfun.GetSemPositionXY(ref a_dPositionX, ref a_dPositionY, ref a_dPositionR);
- }
- #endregion
-
- #region 导出Excel 图片与数据
- /// <summary>
- /// 向Excel中插入图片与数据列表
- /// </summary>
- /// <param name="dtList"></param>
- /// <returns></returns>
- public bool InsertDataToExcelTable(List<System.Data.DataTable> a_list_ElementData,string a_filePath,List<string> a_ImgFilePath)
- {
- m_xe.Create();
- Microsoft.Office.Interop.Excel.Worksheet ws = m_xe.GetSheet("Sheet1");
- m_xe.m_ws = ws;
- string str_ExcelFilePath = a_filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
- m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[0], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, 400, 300);
- m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[1], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 320, 400, 100);
- if (str_ExcelFilePath.IndexOf(":") < 0)
- {
- return false;
- }
- if (m_xe.m_app == null)
- {
- return false;
- }
- //数据列表 行数
- int dataRow = 30;
- //写入标题
- foreach (var dgv in a_list_ElementData)
- {
- for (int i = 0; i < dgv.Columns.Count; i++)
- {
- m_xe.m_ws.Cells[dataRow+1, i + 1] = dgv.Columns[i].ColumnName;
- }
- //写入数值
- for (int r = 0; r < dgv.Rows.Count; r++)
- {
- for (int i = 0; i < dgv.Columns.Count; i++)
- {
- m_xe.m_ws.Cells[dataRow+r + 2, i + 1] = dgv.Rows[r].ItemArray[i];
- }
- System.Windows.Forms.Application.DoEvents();
- }
- }
- //列宽自适应
- m_xe.m_ws.Columns.EntireColumn.AutoFit();
- if (str_ExcelFilePath != "")
- {
- try
- {
- m_xe.m_wb.Saved = true;
- m_xe.m_wb.SaveCopyAs(str_ExcelFilePath);
- }
- catch (Exception ex)
- {
- log.Error("InsertDataToExcelTable错误日志: " + ex.ToString());
- }
- }
- //关闭excel
- m_xe.Close();
- //强行销毁
- GC.Collect();
- //干掉进程里的EXCEL
- foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
- {
- if (p.ProcessName == "EXCEL")
- {
- p.Kill();
- }
- }
- //删除图片文件
- foreach (string imgPath in a_ImgFilePath)
- {
- if (File.Exists(imgPath))
- {
- //如果存在则删除
- File.Delete(imgPath);
- }
- }
- System.Diagnostics.Process.Start(str_ExcelFilePath);
- return true;
- }
- #endregion
- }
- }
|