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();
}
///
/// 计算调试图灰度分布数据
///
///
///
//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;
}
}
///
/// 计算去背景BSE图灰度分布数据
///
///
///
//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;
}
}
///
/// 获取调试图
///
///
///
///
///
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 获取工作距离与放大倍数
///
/// 放大倍数
///
public double GetMagnification()
{
//获取放大倍数
return cfun.GetMagnification();
}
public void GetSemWorkingDistance(ref double a_dWorkingDistance)
{
//获取工作距离
cfun.GetSemWorkingDistance(ref a_dWorkingDistance);
}
#endregion
#region 获取SEM位置
///
/// 获取SEM位置
///
///
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 图片与数据
///
/// 向Excel中插入图片与数据列表
///
///
///
public bool InsertDataToExcelTable(List a_list_ElementData,string a_filePath,List 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
}
}