Imagepro.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. 
  2. using NSOTSController;
  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. //// 算法到此结束,返回结果
  73. return bmp;
  74. }
  75. #endregion
  76. #region 将文件转换成byte[] 数组
  77. /// <summary>
  78. /// 将文件转换成byte[] 数组
  79. /// </summary>
  80. /// <param name="fileUrl">文件路径文件名称</param>
  81. /// <returns>byte[]</returns>
  82. //public static byte[] GetFileData(string fileUrl)
  83. //{
  84. // FileStream pFileStream = null;
  85. // byte[] pReadByte = new byte[0];
  86. // try
  87. // {
  88. // //读取文本文件
  89. // pFileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
  90. // //实例读取字节流对象
  91. // StreamReader sr = new StreamReader(pFileStream, System.Text.Encoding.Default);
  92. // string temp = sr.ReadToEnd();
  93. // //转换byte数组
  94. // byte[] byteArray = System.Text.Encoding.Default.GetBytes(temp);
  95. // //生成字符数组
  96. // string[] tempGroup = temp.Split(',');
  97. // //关闭读取对象
  98. // sr.Close();
  99. // string AppendStr = string.Empty;
  100. // for (int i = 0; i < tempGroup.Length; i++)
  101. // {
  102. // //十进制转十六进制
  103. // AppendStr += Convert.ToString(Convert.ToInt32(tempGroup[i]), 16) + " ";
  104. // }
  105. // //替换字符符号
  106. // AppendStr = AppendStr.Replace(" ", "");
  107. // if ((AppendStr.Length % 2) != 0)
  108. // {
  109. // AppendStr += " ";
  110. // }
  111. // byte[] returnBytes = new byte[AppendStr.Length / 2];
  112. // //转换为byte[]数组
  113. // for (int i = 0; i < returnBytes.Length; i++)
  114. // {
  115. // returnBytes[i] = Convert.ToByte(AppendStr.Substring(i * 2, 2), 16);
  116. // }
  117. // return returnBytes;
  118. // }
  119. // catch
  120. // {
  121. // return pReadByte;
  122. // }
  123. // finally
  124. // {
  125. // if (pFileStream != null)
  126. // pFileStream.Close();
  127. // }
  128. //}
  129. #endregion
  130. #region 根据分辨率获取图像
  131. //public static bool GetScanImage(string ImgWidth, string ImgHeight, string DwellTime, COTSControlFunExport cfun, ref byte[] ImageByte)
  132. //{
  133. // try
  134. // {
  135. // //设置图像分辨率
  136. // int width = 0;
  137. // int height = 0;
  138. // //获取宽度
  139. // width = Convert.ToInt32(ImgWidth);
  140. // height = Convert.ToInt32(ImgHeight);
  141. // int a_ExternalMode = 0;
  142. // //获取终止模式
  143. // a_ExternalMode = cfun.GetSemExternalMode();
  144. // //保存初始模式变量
  145. // int a_oldMode = 0;
  146. // //获取初始模式
  147. // if (!cfun.GetSemScanMode(ref a_oldMode))
  148. // {
  149. // return false;
  150. // }
  151. // //设置当前模式
  152. // if (!cfun.SetSemScanMode(a_ExternalMode))
  153. // {
  154. // return false;
  155. // }
  156. // #region BeamBlank
  157. // int a_nBeamBlank = 0;
  158. // //获取参数
  159. // if (!cfun.GetSemBeamBlank(ref a_nBeamBlank))
  160. // {
  161. // cfun.SetSemScanMode(a_oldMode);
  162. // return false;
  163. // }
  164. // //设置参数
  165. // if (!cfun.SetSemBeamBlank(0))
  166. // {
  167. // cfun.SetSemScanMode(a_oldMode);
  168. // return false;
  169. // }
  170. // #endregion
  171. // #region 获得放大倍数
  172. // //获得放大倍数
  173. // double a_dMagnification = 0;
  174. // //获取参数
  175. // if (!cfun.GetSemMagnification(ref a_dMagnification))
  176. // {
  177. // cfun.SetSemScanMode(a_oldMode);
  178. // return false;
  179. // }
  180. // #endregion
  181. // #region 获取 电镜 X、Y轴 与角度
  182. // //获取 电镜 X、Y轴 与角度
  183. // double PositionX = 0;
  184. // double PositionY = 0;
  185. // double PositionR = 0;
  186. // //获取参数
  187. // if (!cfun.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR))
  188. // {
  189. // cfun.SetSemScanMode(a_oldMode);
  190. // return false;
  191. // }
  192. // #endregion
  193. // #region 设置图像分辨率
  194. // //设置宽度
  195. // if (!cfun.SetImageSize(width, height))
  196. // {
  197. // cfun.SetSemScanMode(a_oldMode);
  198. // return false;
  199. // }
  200. // #endregion
  201. // #region 采集时间
  202. // //采集时间
  203. // int nDwellTime = 16;
  204. // nDwellTime = Convert.ToInt32(DwellTime);
  205. // //nDwellTime = Convert.ToInt32(tbCollectionTime.Text);
  206. // //设置采集时间
  207. // if (!cfun.SetDwellTime(nDwellTime))
  208. // {
  209. // cfun.SetSemScanMode(a_oldMode);
  210. // return false;
  211. // }
  212. // #endregion
  213. // #region MatrixSize
  214. // //获得放大倍数
  215. // int a_MatrixSize = 0;
  216. // Size size = new Size();
  217. // //获取参数
  218. // size = cfun.GetMatrixSize(a_MatrixSize);
  219. // #endregion
  220. // //获取图像数据
  221. // int resultCount = width * height;
  222. // int GetImgCount = cfun.AcquireBSEImage(0, 0, 0, ref ImageByte);
  223. // if (resultCount == GetImgCount)
  224. // {
  225. // //设置为原始 扫描模式
  226. // cfun.SetSemScanMode(a_oldMode);
  227. // }
  228. // else
  229. // {
  230. // cfun.SetSemScanMode(a_oldMode);
  231. // }
  232. // return true;
  233. // }
  234. // catch (Exception ex)
  235. // {
  236. // //记录日志
  237. // //NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  238. // // log.Error(ex.Message.ToString());
  239. // return false;
  240. // }
  241. //}
  242. #endregion
  243. public static int GetScanImage(int iWidth, int iHeigh, string DwellTime, ref byte[] bImageData)
  244. {
  245. //电镜设置对象
  246. COTSControlFunExport cfun = COTSControlFunExport.GetControllerInstance();
  247. int GetImgCount = 0;
  248. try
  249. {
  250. //连接电镜
  251. bool IsConnec = cfun.ConncetSem();
  252. if (!IsConnec)
  253. {
  254. return 0;
  255. }
  256. //实例电镜初始化
  257. bool IsScan = cfun.ScanInit();
  258. if (!IsScan)
  259. {
  260. return 0;
  261. }
  262. int a_ExternalMode = 0;
  263. //获取终止模式
  264. a_ExternalMode = cfun.GetSemExternalMode();
  265. //保存初始模式变量
  266. int a_oldMode = 0;
  267. //获取初始模式
  268. if (!cfun.GetSemScanMode(ref a_oldMode))
  269. {
  270. return 0;
  271. }
  272. //设置当前模式
  273. if (!cfun.SetSemScanMode(a_ExternalMode))
  274. {
  275. return 0;
  276. }
  277. #region BeamBlank
  278. int a_nBeamBlank = 0;
  279. //获取参数
  280. if (!cfun.GetSemBeamBlank(ref a_nBeamBlank))
  281. {
  282. cfun.SetSemScanMode(a_oldMode);
  283. return 0;
  284. }
  285. //设置参数
  286. if (!cfun.SetSemBeamBlank(0))
  287. {
  288. cfun.SetSemScanMode(a_oldMode);
  289. return 0;
  290. }
  291. #endregion
  292. #region 获得放大倍数
  293. //获得放大倍数
  294. double a_dMagnification = 0;
  295. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----begin------");
  296. //获取参数
  297. if (!cfun.GetSemMagnification(ref a_dMagnification))
  298. {
  299. cfun.SetSemScanMode(a_oldMode);
  300. return 0;
  301. }
  302. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification_a_dMagnification:" + a_dMagnification + "------");
  303. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----end------");
  304. #endregion
  305. #region 获取 电镜 X、Y轴 与角度
  306. //获取 电镜 X、Y轴 与角度
  307. double PositionX = 0;
  308. double PositionY = 0;
  309. double PositionR = 0;
  310. //获取参数
  311. if (!cfun.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR))
  312. {
  313. cfun.SetSemScanMode(a_oldMode);
  314. return 0;
  315. }
  316. #endregion
  317. #region 设置图像分辨率
  318. //设置宽度
  319. if (!cfun.SetImageSize(iWidth, iHeigh))
  320. {
  321. cfun.SetSemScanMode(a_oldMode);
  322. return 0;
  323. }
  324. #endregion
  325. #region 采集时间
  326. //采集时间
  327. int nDwellTime = Convert.ToInt32(DwellTime);
  328. //设置采集时间
  329. if (!cfun.SetDwellTime(nDwellTime))
  330. {
  331. cfun.SetSemScanMode(a_oldMode);
  332. return 0;
  333. }
  334. #endregion
  335. #region MatrixSize
  336. //获得放大倍数
  337. int a_MatrixSize = 0;
  338. Size size = new Size();
  339. //获取参数
  340. size = cfun.GetMatrixSize(a_MatrixSize);
  341. #endregion
  342. //获取图像数据
  343. int resultCount = iWidth * iHeigh;
  344. GetImgCount = cfun.AcquireBSEImage(0, 0, 0, ref bImageData);
  345. //记录日志
  346. if (resultCount == GetImgCount)
  347. {
  348. //设置为原始 扫描模式
  349. cfun.SetSemScanMode(a_oldMode);
  350. //Scan类结束
  351. //初始化电镜参数
  352. //if (!cfun.SetAndStartScan())
  353. //{
  354. // return 0;
  355. //}
  356. }
  357. else
  358. {
  359. cfun.SetSemScanMode(a_oldMode);
  360. //记录日志
  361. //初始化电镜参数
  362. //if (!cfun.SetAndStartScan())
  363. //{
  364. // return 0;
  365. //}
  366. }
  367. }
  368. catch (Exception ex)
  369. {
  370. throw ex;
  371. }
  372. finally
  373. {
  374. //cfun.ScanFinishedInstance();
  375. //cfun.DisConnectSem();
  376. //cfun.FreeDll();
  377. }
  378. return GetImgCount;
  379. }
  380. /// <summary>
  381. /// 将byte数组转换为文件并保存到指定地址
  382. /// </summary>
  383. /// <param name="buff">byte数组</param>
  384. /// <param name="savepath">保存地址</param>
  385. //public static void BytesConvertToFile(byte[] buff, string savepath, string fileName)
  386. //{
  387. // try
  388. // {
  389. // //如果不存在就创建Enclosure文件夹
  390. // if (Directory.Exists(savepath + @"\Enclosure\") == false)
  391. // {
  392. // Directory.CreateDirectory(savepath + @"\Enclosure\");
  393. // }
  394. // if (System.IO.File.Exists(savepath + @"\Enclosure\" + fileName))
  395. // {
  396. // System.IO.File.Delete(savepath + @"\Enclosure\" + fileName);
  397. // }
  398. // Bitmap bitmap=ToGrayBitmap(buff, 1024, 768);
  399. // bitmap.Save(savepath + @"\Enclosure\" + fileName);
  400. // }
  401. // catch (Exception)
  402. // {
  403. // }
  404. //}
  405. }
  406. }