Imagepro.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 
  2. using OTSCLRINTERFACE;
  3. using OTSModelSharp.ServiceInterface;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace OTSSysMgrApp
  14. {
  15. public class Imagepro
  16. {
  17. public Imagepro()
  18. {
  19. }
  20. #region 通过byte数组生成BMP图像文件
  21. /// <summary>
  22. /// 将一个byte的数组转换为8bit灰度位图
  23. /// </summary>
  24. /// <param name="data">数组</param>
  25. /// <param name="width">图像宽度</param>
  26. /// <param name="height">图像高度</param>
  27. /// <returns>位图</returns>
  28. public static Bitmap ToGrayBitmap(byte[] data, int width, int height)
  29. {
  30. //// 申请目标位图的变量,并将其内存区域锁定
  31. Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
  32. //// BitmapData这部分内容 需要 using System.Drawing.Imaging;
  33. BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
  34. ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
  35. //// 获取图像参数
  36. // 扫描线的宽度
  37. int stride = bmpData.Stride;
  38. // 显示宽度与扫描线宽度的间隙
  39. int offset = stride - width;
  40. // 获取bmpData的内存起始位置
  41. IntPtr iptr = bmpData.Scan0;
  42. // 用stride宽度,表示这是内存区域的大小
  43. int scanBytes = stride * height;
  44. //// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
  45. int posScan = 0;
  46. int posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组
  47. byte[] pixelValues = new byte[scanBytes]; //为目标数组分配内存
  48. //for (int x = height-1;x>=0 ; x--) data[startIndex+ y];//
  49. for (int x = 0; x < height; x++)
  50. {
  51. int startIndex = x * width;
  52. //// 下面的循环节是模拟行扫描
  53. for (int y = 0; y < width; y++)
  54. {
  55. pixelValues[posScan++] = data[posReal++];
  56. }
  57. posScan += offset; //行扫描结束,要将目标位置指针移过那段“间隙”
  58. }
  59. //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
  60. System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
  61. bmp.UnlockBits(bmpData); // 解锁内存区域
  62. //// 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度
  63. ColorPalette tempPalette;
  64. using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
  65. {
  66. tempPalette = tempBmp.Palette;
  67. }
  68. for (int i = 0; i < 256; i++)
  69. {
  70. tempPalette.Entries[i] = Color.FromArgb(i, i, i);
  71. }
  72. bmp.Palette = tempPalette;
  73. return bmp;
  74. }
  75. #endregion
  76. public static int GetScanImage(int iWidth, int iHeigh, string DwellTime, ref byte[] bImageData)
  77. {
  78. if(FileHelper.GetXMLInformations("SemControllerName") == "FEI")
  79. {
  80. ScanController m_ScanHardwareMgr = ScanController.GetScanController();
  81. m_ScanHardwareMgr.Init();
  82. m_ScanHardwareMgr.SetImageSize(iWidth, iHeigh);
  83. bImageData = m_ScanHardwareMgr.AcquireBSEImage(0, 0, int.Parse(DwellTime)).GetImageDataPtr();
  84. return 1;
  85. }
  86. else
  87. {
  88. //电镜设置对象
  89. var cfun = COTSControlFunExport.GetControllerInstance();
  90. int GetImgCount = 0;
  91. try
  92. {
  93. //连接电镜
  94. bool IsConnec = cfun.ConncetSem();
  95. if (!IsConnec)
  96. {
  97. return 0;
  98. }
  99. //实例电镜初始化
  100. bool IsScan = cfun.ScanInit();
  101. if (!IsScan)
  102. {
  103. return 0;
  104. }
  105. int a_ExternalMode = 0;
  106. //获取终止模式
  107. a_ExternalMode = cfun.GetSemExternalMode();
  108. //保存初始模式变量
  109. int a_oldMode = 0;
  110. //获取初始模式
  111. if (!cfun.GetSemScanMode(ref a_oldMode))
  112. {
  113. return 0;
  114. }
  115. //设置当前模式
  116. if (!cfun.SetSemScanMode(a_ExternalMode))
  117. {
  118. return 0;
  119. }
  120. #region BeamBlank
  121. int a_nBeamBlank = 0;
  122. //获取参数
  123. if (!cfun.GetSemBeamBlank(ref a_nBeamBlank))
  124. {
  125. cfun.SetSemScanMode(a_oldMode);
  126. return 0;
  127. }
  128. //设置参数
  129. if (!cfun.SetSemBeamBlank(false))
  130. {
  131. cfun.SetSemScanMode(a_oldMode);
  132. return 0;
  133. }
  134. #endregion
  135. #region 获得放大倍数
  136. //获得放大倍数
  137. double a_dMagnification = 0;
  138. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----begin------");
  139. //获取参数
  140. if (!cfun.GetSemMagnification(ref a_dMagnification))
  141. {
  142. cfun.SetSemScanMode(a_oldMode);
  143. return 0;
  144. }
  145. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification_a_dMagnification:" + a_dMagnification + "------");
  146. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----end------");
  147. #endregion
  148. #region 获取 电镜 X、Y轴 与角度
  149. //获取 电镜 X、Y轴 与角度
  150. double PositionX = 0;
  151. double PositionY = 0;
  152. double PositionR = 0;
  153. //获取参数
  154. if (!cfun.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR))
  155. {
  156. cfun.SetSemScanMode(a_oldMode);
  157. return 0;
  158. }
  159. #endregion
  160. #region 设置图像分辨率
  161. //设置宽度
  162. if (!cfun.SetImageSize(iWidth, iHeigh))
  163. {
  164. cfun.SetSemScanMode(a_oldMode);
  165. return 0;
  166. }
  167. #endregion
  168. #region 采集时间
  169. //采集时间
  170. int nDwellTime = Convert.ToInt32(DwellTime);
  171. //设置采集时间
  172. if (!cfun.SetDwellTime(nDwellTime))
  173. {
  174. cfun.SetSemScanMode(a_oldMode);
  175. return 0;
  176. }
  177. #endregion
  178. #region MatrixSize
  179. //获得放大倍数
  180. int a_MatrixSize = 0;
  181. Size size = new Size();
  182. //获取参数
  183. size = cfun.GetMatrixSize(a_MatrixSize);
  184. #endregion
  185. //获取图像数据
  186. int resultCount = iWidth * iHeigh;
  187. GetImgCount = cfun.AcquireBSEImage(0, 0, 0, ref bImageData);
  188. //记录日志
  189. if (resultCount == GetImgCount)
  190. {
  191. //设置为原始 扫描模式
  192. cfun.SetSemScanMode(a_oldMode);
  193. }
  194. else
  195. {
  196. cfun.SetSemScanMode(a_oldMode);
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. throw ex;
  202. }
  203. return GetImgCount;
  204. }
  205. }
  206. }
  207. }