Imagepro.cs 7.6 KB

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