ImageDispose.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using NSLogFunExport;
  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 ImageDispose
  15. {
  16. public ImageDispose()
  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, CFunExportClass cfunClass, 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))
  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. cfunClass.TraceErrorLog(ex.Message.ToString());
  238. return false;
  239. }
  240. }
  241. #endregion
  242. public static int GetScanImage(int iWidth, int iHeigh, string DwellTime, ref byte[] bImageData)
  243. {
  244. //电镜设置对象
  245. COTSControlFunExport cfun = new COTSControlFunExport("SyaMgrAPP_GetScanImage");
  246. int GetImgCount = 0;
  247. try
  248. {
  249. //连接电镜
  250. bool IsConnec = cfun.ConncetSem();
  251. if (!IsConnec)
  252. {
  253. return 0;
  254. }
  255. //实例电镜初始化
  256. bool IsScan = cfun.ScanInit();
  257. if (!IsScan)
  258. {
  259. return 0;
  260. }
  261. int a_ExternalMode = 0;
  262. //获取终止模式
  263. a_ExternalMode = cfun.GetSemExternalMode();
  264. //保存初始模式变量
  265. int a_oldMode = 0;
  266. //获取初始模式
  267. if (!cfun.GetSemScanMode(ref a_oldMode))
  268. {
  269. return 0;
  270. }
  271. //设置当前模式
  272. if (!cfun.SetSemScanMode(a_ExternalMode))
  273. {
  274. return 0;
  275. }
  276. #region BeamBlank
  277. int a_nBeamBlank = 0;
  278. //获取参数
  279. if (!cfun.GetSemBeamBlank(ref a_nBeamBlank))
  280. {
  281. cfun.SetSemScanMode(a_oldMode);
  282. return 0;
  283. }
  284. //设置参数
  285. if (!cfun.SetSemBeamBlank(0))
  286. {
  287. cfun.SetSemScanMode(a_oldMode);
  288. return 0;
  289. }
  290. #endregion
  291. #region 获得放大倍数
  292. //获得放大倍数
  293. double a_dMagnification = 0;
  294. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----begin------");
  295. //获取参数
  296. if (!cfun.GetSemMagnification(ref a_dMagnification))
  297. {
  298. cfun.SetSemScanMode(a_oldMode);
  299. return 0;
  300. }
  301. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification_a_dMagnification:" + a_dMagnification + "------");
  302. //m_MsrApp.m_LogFunExport.TraceLog("APP_GetSemMagnification-----end------");
  303. #endregion
  304. #region 获取 电镜 X、Y轴 与角度
  305. //获取 电镜 X、Y轴 与角度
  306. double PositionX = 0;
  307. double PositionY = 0;
  308. double PositionR = 0;
  309. //获取参数
  310. if (!cfun.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR))
  311. {
  312. cfun.SetSemScanMode(a_oldMode);
  313. return 0;
  314. }
  315. #endregion
  316. #region 设置图像分辨率
  317. //设置宽度
  318. if (!cfun.SetImageSize(iWidth))
  319. {
  320. cfun.SetSemScanMode(a_oldMode);
  321. return 0;
  322. }
  323. #endregion
  324. #region 采集时间
  325. //采集时间
  326. int nDwellTime = Convert.ToInt32(DwellTime);
  327. //设置采集时间
  328. if (!cfun.SetDwellTime(nDwellTime))
  329. {
  330. cfun.SetSemScanMode(a_oldMode);
  331. return 0;
  332. }
  333. #endregion
  334. #region MatrixSize
  335. //获得放大倍数
  336. int a_MatrixSize = 0;
  337. Size size = new Size();
  338. //获取参数
  339. size = cfun.GetMatrixSize(a_MatrixSize);
  340. #endregion
  341. //获取图像数据
  342. int resultCount = iWidth * iHeigh;
  343. GetImgCount = cfun.AcquireBSEImage(0, 0, 0, ref bImageData);
  344. //记录日志
  345. if (resultCount == GetImgCount)
  346. {
  347. //设置为原始 扫描模式
  348. cfun.SetSemScanMode(a_oldMode);
  349. //Scan类结束
  350. //初始化电镜参数
  351. if (!cfun.SetAndStartScan())
  352. {
  353. return 0;
  354. }
  355. }
  356. else
  357. {
  358. cfun.SetSemScanMode(a_oldMode);
  359. //记录日志
  360. //初始化电镜参数
  361. if (!cfun.SetAndStartScan())
  362. {
  363. return 0;
  364. }
  365. }
  366. }
  367. catch (Exception ex)
  368. {
  369. throw ex;
  370. }
  371. finally
  372. {
  373. cfun.ScanFinishedInstance();
  374. cfun.DisConnectSem();
  375. //cfun.FreeDll();
  376. }
  377. return GetImgCount;
  378. }
  379. /// <summary>
  380. /// 将byte数组转换为文件并保存到指定地址
  381. /// </summary>
  382. /// <param name="buff">byte数组</param>
  383. /// <param name="savepath">保存地址</param>
  384. public static void BytesConvertToFile(byte[] buff, string savepath, string fileName)
  385. {
  386. try
  387. {
  388. //如果不存在就创建Enclosure文件夹
  389. if (Directory.Exists(savepath + @"\Enclosure\") == false)
  390. {
  391. Directory.CreateDirectory(savepath + @"\Enclosure\");
  392. }
  393. if (System.IO.File.Exists(savepath + @"\Enclosure\" + fileName))
  394. {
  395. System.IO.File.Delete(savepath + @"\Enclosure\" + fileName);
  396. }
  397. Bitmap bitmap=ToGrayBitmap(buff, 1024, 768);
  398. bitmap.Save(savepath + @"\Enclosure\" + fileName);
  399. }
  400. catch (Exception)
  401. {
  402. }
  403. }
  404. }
  405. }