OTSMeasureStatusWindow.cs 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. 
  2. using OpenCvSharp.Extensions;
  3. using OTS.WinFormsUI.Docking;
  4. using OTSCLRINTERFACE;
  5. using OTSDataType;
  6. using OTSIncAGraph.Controls;
  7. using OTSMeasureApp._0_OTSModel.OTSDataType;
  8. using OTSModelSharp;
  9. using OTSModelSharp.ServiceCenter;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Drawing;
  15. using System.Drawing.Imaging;
  16. using System.IO;
  17. using System.Reflection;
  18. using System.Windows.Forms;
  19. //using OTSMeasureApp._0_OTSModel.Measure.ParamData;
  20. namespace OTSMeasureApp
  21. {
  22. public enum idLine
  23. {
  24. //曲线
  25. GrayLine = 0,
  26. //灰度图
  27. GrayImage = 1
  28. }
  29. public enum MeasureStateMenuType
  30. {
  31. //获取BSE图
  32. ReadBSEPic = 0,
  33. //设置可视化灰度
  34. SetVisualGray = 1,
  35. //显示BSE灰度曲线图
  36. SetVisualGrayForSpecialGray = 2,
  37. grayToolStripMenuItem = 3,
  38. //切换至BSE图
  39. ChangeDiffImageShow = 4,
  40. //BSE去背景图
  41. DelBSEBG = 5,
  42. ColoredBSENoBG = 6,
  43. AIProcess=7,
  44. //显示BSE去背景灰度曲线图
  45. SampleHoleBSEImage = 8,
  46. //线扫描曲线
  47. MenuItemLineScam = 9,
  48. //单点采集Xray与元素信息
  49. PointScanElementMenuItem = 10,
  50. //导出采集信息报告
  51. ExportReport = 11
  52. }
  53. public partial class OTSMeasureStatusWindow : DockContent
  54. {
  55. #region 全部变量
  56. public OTSIncAMeasureAppForm m_MeasureAppForm;
  57. /* 接收图像数据 */
  58. //public byte[] Imagedata = new byte[m_iWidth];
  59. static int m_iWidth = 0;
  60. static int m_iHeight = 0;
  61. //获取BSE图时最初的位置
  62. Point m_GetBSEImageInitPoint = new Point();
  63. // 调试图图片数据
  64. public byte[] originalBseData;
  65. //去背景图
  66. private byte[] removeBGBseData;
  67. private Bitmap coloredParticleImage;
  68. //灰度图数据
  69. public double[] dGrayLevelData;
  70. //灰度峰值
  71. private double dGrayLevelMax;
  72. //去背景灰度图数据
  73. private double[] dGrayAbandonLevelData;
  74. //去背景灰度峰值
  75. private double dGrayAbandonLevelMax;
  76. public string FileName;
  77. //原图Y轴
  78. int YSize = 0;
  79. //int drawflag = 0;
  80. private int height = 0;
  81. private int width = 0;
  82. //画布大小
  83. private Bitmap bitmap;
  84. //要画曲线的点y坐标值,x值为10s采集一次数据
  85. private float[] curvebuf = new float[] { 20.0f, 30.0f, 40.0f, 35.4f, 21.6f, 32.8f, 5.2f, 9.9f, 30f, 35.3f, 25.3f, 21.2f, 30.2f, 22.8f, 40.5f, 5.6f, 20.6f };
  86. //记录鼠标在BSE移动时 图像的位置
  87. double m_MouseMoveImageX = -1;
  88. double m_MouseMoveImageY = -1;
  89. //绘制采集点对象
  90. public static List<CRectangleGDIObject> m_ElementPointGDIObjects;
  91. Point m_mouseCurrentPoint = new Point();
  92. //鼠标缩放倍数
  93. float m_CurrentMultiple = 0;
  94. float m_Multiple = 0.2f;
  95. int m_MaxMultipleRange = 4;
  96. //单击采集的鼠标位置
  97. double dMouseImgX = 0;
  98. double dMouseImgY = 0;
  99. NLog.Logger log;
  100. //国际化
  101. OTSCommon.Language lan;
  102. Hashtable table;
  103. #endregion
  104. #region 在右下角状态栏中显示鼠标当前位置所需要的参数
  105. double dPositionX = 0;
  106. double dPositionY = 0;
  107. int FieldWitdh = 0;
  108. double FieldHeight = 0;
  109. #endregion
  110. public enum ImageType
  111. {
  112. //BSE图
  113. BSEImage = 0,
  114. //去背景图
  115. RemoveBGImage = 1,
  116. //显示灰度曲线图
  117. GrayLevelChart = 2,
  118. //线扫描曲线
  119. GrayLevelLine = 3,
  120. //显示去背景BSE图灰度曲线
  121. GrayAbandonLevelChart = 4,
  122. //
  123. ColorParticleImage = 5
  124. }
  125. //图bfResult
  126. public static bool bfResult = false;
  127. //当前是否显示线扫描
  128. public static bool IsShowGrayLevelLine = false;
  129. //当前默认值
  130. ImageType m_imagetype = ImageType.BSEImage;
  131. ImageType m_IsShowImageType = ImageType.BSEImage;
  132. public OTSMeasureStatusWindow(OTSIncAMeasureAppForm MeasureApp)
  133. {
  134. InitializeComponent();
  135. m_MeasureAppForm = MeasureApp;
  136. m_ElementPointGDIObjects = new List<CRectangleGDIObject>();
  137. //if (OTSCommon.Language.ReadDefaultLanguage() == "ZH")
  138. //{
  139. // contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Text = "显示至BSE原图";
  140. //}
  141. //else
  142. //{
  143. // contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Text = "SwitchDisplayToBSEDiagram";
  144. //}
  145. //进入时首先对XRayTable进行隐藏
  146. panelXray.Visible = false;
  147. //国际化
  148. lan = new OTSCommon.Language(this);
  149. table = lan.GetNameTable(this.Name);
  150. }
  151. /// <summary>
  152. /// 在控件中显示BSE图像
  153. /// </summary>
  154. /// <param name="m_iWidth"></param>
  155. /// <param name="m_iHeigh"></param>
  156. /// <param name="bBseData"></param>
  157. public void ShowImage(int m_iWidth, int m_iHeigh, byte[] bBseData)
  158. {
  159. try
  160. {
  161. Bitmap bitmap = CImageHandler.ToGrayBitmap(bBseData, m_iWidth, m_iHeigh);
  162. ShowBitmap(m_iWidth, m_iHeigh, bitmap);
  163. }
  164. catch(Exception e)
  165. {
  166. NLog.LogManager.GetCurrentClassLogger().Error(e.Message);
  167. }
  168. }
  169. public void ShowBitmap(int m_iWidth, int m_iHeigh, Bitmap coloredImage)
  170. {
  171. picBox.Image = coloredImage;
  172. //设置Pictrue显示位置
  173. picBox.Height = this.RestoreBounds.Height;
  174. picBox.Width = this.RestoreBounds.Height * 4 / 3;
  175. //获取黄线坐标位置
  176. Rectangle rectangle = GetPictureBoxZoomSize(picBox);
  177. m_GetBSEImageInitPoint = rectangle.Location;
  178. pointStart.X = rectangle.X;
  179. pointEnd.X = rectangle.X + rectangle.Width;
  180. pointStart.Y = rectangle.Y + (rectangle.Height) / 2;
  181. pointEnd.Y = rectangle.Y + (rectangle.Height) / 2;
  182. picBox.Left = 0;
  183. picBox.Top = 0;
  184. picBox.Show();
  185. }
  186. /// <summary>
  187. /// 在控件中显示图像
  188. /// </summary>
  189. /// <param name="m_iWidth"></param>
  190. /// <param name="m_iHeigh"></param>
  191. /// <param name="bBseData"></param>
  192. public Image ShowImageStatus(int m_iWidth, int m_iHeigh, byte[] bBseData)
  193. {
  194. return CImageHandler.ToGrayBitmap(bBseData, m_iWidth, m_iHeigh);
  195. }
  196. //显示扫描曲线图方法
  197. #region 显示扫描曲线图方法
  198. private void DrawCurve(byte[] buf, PaintEventArgs e)
  199. {
  200. //创建位图
  201. Rectangle rectangle = GetPictureBoxZoomSize(picBox);
  202. double rate = (double)rectangle.Height / (double)picBox.Image.Height;
  203. width = rectangle.Width;
  204. height = rectangle.Height;
  205. bitmap = new Bitmap(width, height);
  206. Pen mypenYellow = new Pen(Color.Yellow, 0.001f);//点颜色
  207. int len = buf.Length;
  208. PointF[] CurvePointF = new PointF[len];//坐标点
  209. float pointX = 0;
  210. float pointY = 0;
  211. for (int i = 0; i < buf.Length; i++)
  212. {
  213. pointX = (picBox.Width - rectangle.Width) / 2 + Convert.ToSingle(i * rate);
  214. float imagei = (int)GrayLevel.Max - Convert.ToInt32(buf[i]);
  215. float imagerate = (Convert.ToSingle(Convert.ToDouble(rectangle.Height) / 510));
  216. pointY = imagei * imagerate + (rectangle.Height) / 4+rectangle.Top;
  217. if (pointY > ((rectangle.Height) * 3 / 4))
  218. {
  219. float ii = pointY;
  220. }
  221. CurvePointF[i] = new PointF(pointX, pointY);
  222. }
  223. float Tension = 0.001f;
  224. e.Graphics.DrawCurve(mypenYellow, CurvePointF, Tension);//画曲线
  225. }
  226. #endregion
  227. //处理获取调试图菜单事件
  228. #region 处理获取调试图菜单事件
  229. private void AcquireBSEImage_Click(object sender, EventArgs e)
  230. {
  231. if (m_ElementPointGDIObjects != null)
  232. {
  233. m_ElementPointGDIObjects.Clear();
  234. }
  235. //设置右键菜单中的项 是否可编辑
  236. SetContextMenuEnabled(true);
  237. AcquireBSEImage();
  238. DelElementPointGDIObjects();
  239. panelXray.Visible = false;
  240. m_CurrentMultiple = 0;
  241. var name0 = contextMenuStrip1.Items[0].Name;
  242. var name7 = contextMenuStrip1.Items[7].Name;
  243. var name8 = contextMenuStrip1.Items[8].Name;
  244. contextMenuStrip1.Items[(int)MeasureStateMenuType.AIProcess].Enabled = true;
  245. }
  246. /// <summary>
  247. /// 获取BSE图方法
  248. /// </summary>
  249. private void AcquireBSEImage()
  250. {
  251. //获取电镜中图像大小
  252. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolutionStr();// 举例:1024X768
  253. string[] sArray = str.Split('X');
  254. if (sArray[0] != "" && sArray[1] != "")
  255. {
  256. m_iWidth = Convert.ToInt32(sArray[0]);
  257. m_iHeight = Convert.ToInt32(sArray[1]);
  258. }
  259. //建立图像数据
  260. originalBseData = new byte[m_iWidth * m_iHeight];
  261. //获取图像数据
  262. int nDwellTime = 4;
  263. try
  264. {
  265. int level = (int)m_MeasureAppForm.m_ProjParam.GetResultData().GetWorkingSample().GetMsrParams().GetImageScanParam().GetScanImageSpeed();
  266. switch (level)
  267. {
  268. case 0:
  269. nDwellTime = 2;
  270. break;
  271. case 1:
  272. nDwellTime = 4;
  273. break;
  274. case 2:
  275. nDwellTime = 8;
  276. break;
  277. }
  278. }
  279. catch (Exception)
  280. {
  281. nDwellTime = 8;
  282. }
  283. bool rst = GetScanImage(m_iWidth, m_iHeight, nDwellTime, ref originalBseData);
  284. //取图不成功就返回
  285. if (rst)
  286. {
  287. bfResult = true;
  288. }
  289. if (!rst)
  290. {
  291. if (contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled == true)
  292. {
  293. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = false;
  294. }
  295. //显示单点扫描元素信息菜单
  296. contextMenuStrip1.Items[(int)MeasureStateMenuType.PointScanElementMenuItem].Enabled = false;
  297. picBox.Hide();
  298. return;
  299. }
  300. else
  301. {
  302. //显示单点扫描元素信息菜单
  303. contextMenuStrip1.Items[(int)MeasureStateMenuType.PointScanElementMenuItem].Enabled = true;
  304. }
  305. //获取灰度图数据
  306. dGrayLevelData = new double[(int)GrayLevel.Max];
  307. dGrayLevelData = GetGaryData(originalBseData, dGrayLevelData);
  308. //计算灰度最大值
  309. dGrayLevelMax = dGrayLevelData[0];
  310. for (int i = 1; i < (int)GrayLevel.Max; i++)
  311. {
  312. dGrayLevelMax = Math.Max(dGrayLevelMax, dGrayLevelData[i]);
  313. }
  314. SetClickMenuBSEImg();
  315. //更新当前的图
  316. //UpdateImageOrChart(m_imagetype);
  317. ShowImage(m_iWidth, m_iHeight, originalBseData);
  318. IsShowGrayLevelLine = false;
  319. //获取默认Y轴
  320. myChart.Visible = false;
  321. //图像加载后的显示位置
  322. m_CurrentMultiple = 0;
  323. picBox.Height = this.RestoreBounds.Height;
  324. picBox.Left = 0;
  325. picBox.Top = 0;
  326. }
  327. public double[] GetGaryData(byte[] Imagedata, double[] GrayLevelData)
  328. {
  329. //获得下标每一点的灰度值,并在数组里加一
  330. for (int i = 0; i < Imagedata.Length; i++)
  331. {
  332. byte graylevel = Imagedata[i];
  333. if (graylevel > 0 && graylevel < 255)
  334. {
  335. GrayLevelData[graylevel] += 1;
  336. }
  337. }
  338. return GrayLevelData;
  339. }
  340. public bool GetScanImage(int iWidth, int iHeight, int iDwellTime, ref byte[] bImageData)
  341. {
  342. //电镜设置对象
  343. IScanController cfun = ScanController.GetScanController();
  344. ISemController sem = SemController.GetSEMController();
  345. //int GetImgCount = 0;
  346. try
  347. {
  348. //连接电镜
  349. bool IsConnec = cfun.Init();
  350. if (!IsConnec)
  351. {
  352. string strErrorInfo = "(OTSBSEImageFun.GetScanImage) cfun.ConncetSem() = false Failed";
  353. log.Error(strErrorInfo);
  354. return false;
  355. }
  356. cfun.SetImageSize(iWidth, iHeight);
  357. cfun.SetDwellTime(iDwellTime);
  358. var bse = cfun.AcquireBSEImage( );
  359. bImageData = bse.GetImageDataPtr();
  360. sem.SetScanExternal(false);
  361. }
  362. catch (Exception ex)
  363. {
  364. log.Error(ex.Message.ToString());
  365. return false;
  366. }
  367. return true;
  368. }
  369. #endregion
  370. #region 设置点击BSE菜单
  371. public void SetClickMenuBSEImg()
  372. {
  373. //用于线扫描曲线
  374. m_IsShowImageType = ImageType.BSEImage;
  375. m_imagetype = ImageType.BSEImage;
  376. picBox.Show();
  377. }
  378. #endregion
  379. //显示去背景图 = AUTO
  380. #region 显示去背景图
  381. private void ShowRemoveBGImage_Click(object sender, EventArgs e)
  382. {
  383. //设置右键菜单中的项 是否可编辑
  384. SetContextMenuEnabled(true);
  385. ShowRemoveBGImage();
  386. m_CurrentMultiple = 0;
  387. panelXray.Visible = false;
  388. }
  389. protected void ShowRemoveBGImage()
  390. {
  391. //获取电镜中图像大小
  392. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolutionStr();
  393. string[] sArray = str.Split('X');
  394. if (sArray[0] != "" && sArray[1] != "")
  395. {
  396. m_iWidth = Convert.ToInt32(sArray[0]);
  397. m_iHeight = Convert.ToInt32(sArray[1]);
  398. }
  399. //获取图像数据
  400. COTSSample WSample = m_MeasureAppForm.m_ProjData.GetWorkingSample();
  401. double pixelSize = WSample.CalculatePixelSize();
  402. var imageProcessParam = WSample.GetMsrParams().GetImageProcessParam();
  403. var imageHandle = new CImageHandler();
  404. bfResult = imageHandle.GetBSEImage(imageProcessParam, pixelSize, originalBseData, m_iWidth, m_iHeight, ref removeBGBseData);
  405. m_IsShowImageType = ImageType.RemoveBGImage;
  406. //获取去背景灰度图数据
  407. dGrayAbandonLevelData = new double[(int)GrayLevel.Max];
  408. dGrayAbandonLevelData = GetGrayAbandonData(removeBGBseData, dGrayAbandonLevelData);
  409. //计算灰度去背景最大值
  410. dGrayAbandonLevelMax = dGrayAbandonLevelData[0];
  411. for (int i = 1; i < (int)GrayLevel.Max; i++)
  412. {
  413. dGrayAbandonLevelMax = Math.Max(dGrayAbandonLevelMax, dGrayAbandonLevelData[i]);
  414. }
  415. //取图不成功就返回
  416. if (!bfResult) { return; }
  417. ShowImage(m_iWidth, m_iHeight, removeBGBseData);
  418. IsShowGrayLevelLine = false;
  419. myChart.Visible = false;
  420. //切换显示至BSE图
  421. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = true;
  422. }
  423. protected void ShowColoredRemoveBGImage()
  424. {
  425. //获取电镜中图像大小
  426. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolutionStr();
  427. string[] sArray = str.Split('X');
  428. if (sArray[0] != "" && sArray[1] != "")
  429. {
  430. m_iWidth = Convert.ToInt32(sArray[0]);
  431. m_iHeight = Convert.ToInt32(sArray[1]);
  432. }
  433. //获取图像数据
  434. COTSSample WSample = m_MeasureAppForm.m_ProjData.GetWorkingSample();
  435. double pixelSize = WSample.CalculatePixelSize();
  436. var imageProcessParam = WSample.GetMsrParams().GetImageProcessParam();
  437. var imageHandle = new CImageHandler();
  438. coloredParticleImage = new Bitmap(m_iWidth, m_iHeight);
  439. CBSEImgClr pImgNoBG = new CBSEImgClr(new Rectangle(0, 0, m_iWidth, m_iHeight));
  440. bfResult = imageHandle.GetColoredImage(imageProcessParam, pixelSize, originalBseData, m_iWidth, m_iHeight, ref pImgNoBG, ref coloredParticleImage);
  441. removeBGBseData = pImgNoBG.GetImageDataPtr();
  442. //获取去背景灰度图数据
  443. dGrayAbandonLevelData = new double[(int)GrayLevel.Max];
  444. dGrayAbandonLevelData = GetGrayAbandonData(removeBGBseData, dGrayAbandonLevelData);
  445. //计算灰度去背景最大值
  446. dGrayAbandonLevelMax = dGrayAbandonLevelData[0];
  447. for (int i = 1; i < (int)GrayLevel.Max; i++)
  448. {
  449. dGrayAbandonLevelMax = Math.Max(dGrayAbandonLevelMax, dGrayAbandonLevelData[i]);
  450. }
  451. //取图不成功就返回
  452. if (!bfResult) { return; }
  453. ShowBitmap(m_iWidth, m_iHeight, coloredParticleImage);
  454. IsShowGrayLevelLine = false;
  455. myChart.Visible = false;
  456. picBox.Show();
  457. //切换显示至BSE图
  458. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = true;
  459. if (OTSCommon.Language.ReadDefaultLanguage() == "ZH")
  460. {
  461. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Text = "切换显示至BSE图";
  462. }
  463. else
  464. {
  465. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Text = "SwitchDisplayToBSEDiagram";
  466. }
  467. }
  468. protected void ShowColoredRemoveBGImage_Ai(byte[] originalBseData)
  469. {
  470. //获取电镜中图像大小
  471. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolutionStr();
  472. string[] sArray = str.Split('X');
  473. if (sArray[0] != "" && sArray[1] != "")
  474. {
  475. m_iWidth = Convert.ToInt32(sArray[0]);
  476. m_iHeight = Convert.ToInt32(sArray[1]);
  477. }
  478. //获取图像数据
  479. COTSSample WSample = m_MeasureAppForm.m_ProjData.GetWorkingSample();
  480. double pixelSize = WSample.CalculatePixelSize();
  481. var imageProcessParam = WSample.GetMsrParams().GetImageProcessParam();
  482. var imageHandle = new CImageHandler();
  483. coloredParticleImage = new Bitmap(m_iWidth, m_iHeight);
  484. CBSEImgClr pImgNoBG = new CBSEImgClr(new Rectangle(0, 0, m_iWidth, m_iHeight));
  485. bfResult = imageHandle.GetColoredImage(imageProcessParam, pixelSize, originalBseData, m_iWidth, m_iHeight, ref pImgNoBG, ref coloredParticleImage);
  486. removeBGBseData = pImgNoBG.GetImageDataPtr();
  487. //获取去背景灰度图数据
  488. dGrayAbandonLevelData = new double[(int)GrayLevel.Max];
  489. dGrayAbandonLevelData = GetGrayAbandonData(removeBGBseData, dGrayAbandonLevelData);
  490. //计算灰度去背景最大值
  491. dGrayAbandonLevelMax = dGrayAbandonLevelData[0];
  492. for (int i = 1; i < (int)GrayLevel.Max; i++)
  493. {
  494. dGrayAbandonLevelMax = Math.Max(dGrayAbandonLevelMax, dGrayAbandonLevelData[i]);
  495. }
  496. //取图不成功就返回
  497. if (!bfResult) { return; }
  498. ShowBitmap(m_iWidth, m_iHeight, coloredParticleImage);
  499. IsShowGrayLevelLine = false;
  500. myChart.Visible = false;
  501. picBox.Show();
  502. //切换显示至BSE图
  503. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = true;
  504. if (OTSCommon.Language.ReadDefaultLanguage() == "ZH")
  505. {
  506. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Text = "切换显示至BSE图";
  507. }
  508. else
  509. {
  510. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Text = "SwitchDisplayToBSEDiagram";
  511. }
  512. }
  513. #endregion
  514. //显示灰度曲线
  515. #region 显示灰度曲线
  516. private void ShowGrayLevelImage_Click(object sender, EventArgs e)
  517. {
  518. //设置右键菜单中的项 是否可编辑
  519. SetContextMenuEnabled(false);
  520. ShowGrayLevelImage();
  521. myChart.Visible = true;
  522. panelXray.Visible = false;
  523. IsShowGrayLevelLine = false;
  524. }
  525. protected void ShowGrayLevelImage()
  526. {
  527. m_imagetype = ImageType.GrayLevelChart;
  528. picBox.Hide();
  529. //切换显示至BSE图
  530. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = true;
  531. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = true;
  532. if (OTSCommon.Language.ReadDefaultLanguage() == "ZH")
  533. {
  534. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Text = "切换显示至BSE图";
  535. }
  536. else
  537. {
  538. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Text = "SwitchDisplayToBSEDiagram";
  539. }
  540. //UpdateImageOrChart(m_imagetype);
  541. int[] XData = new int[(int)GrayLevel.Max];
  542. for (int i = 0; i < (int)GrayLevel.Max; i++)
  543. {
  544. XData[i] = i + 1;
  545. }
  546. int[] YData = new int[dGrayLevelData.Length];
  547. for (int i = 0; i < dGrayLevelData.Length; i++)
  548. {
  549. YData[i] = (int)dGrayLevelData[i];
  550. }
  551. chartData(XData, YData);
  552. }
  553. #endregion
  554. //显示线扫描曲线
  555. #region 显示线扫描曲线
  556. private void ShowGrayLevelLine_Click(object sender, EventArgs e)
  557. {
  558. if (MenuItemLineScan.Checked)
  559. {
  560. IsShowGrayLevelLine = true;
  561. }
  562. else
  563. {
  564. IsShowGrayLevelLine = false;
  565. }
  566. panelXray.Visible = false;
  567. picBox.Refresh();
  568. }
  569. #endregion
  570. #region 扫描线轴X
  571. public Rectangle GetPictureBoxZoomSize(PictureBox p_PictureBox)
  572. {
  573. if (p_PictureBox != null)
  574. {
  575. PropertyInfo _ImageRectanglePropert = p_PictureBox.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
  576. return (Rectangle)_ImageRectanglePropert.GetValue(p_PictureBox, null);
  577. }
  578. return new Rectangle(0, 0, 0, 0);
  579. }
  580. #endregion
  581. private void OTSMeasureStatusWindow_Load(object sender, EventArgs e)
  582. {
  583. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(OTSMeasureStatusWindow));
  584. this.control_XRayTable1 = new Control_XRayTable();
  585. this.control_XRayTable1.BackColor = System.Drawing.Color.FromArgb(180, 255, 255);
  586. this.control_XRayTable1.Dock = System.Windows.Forms.DockStyle.Fill;
  587. this.control_XRayTable1.GBInfoStr = "";
  588. this.control_XRayTable1.GoodChineseName = "";
  589. this.control_XRayTable1.GoodName = "";
  590. this.control_XRayTable1.List_ShowElementInfo = ((System.Collections.Generic.List<OTSIncAGraph.Controls.ShowElementInfo>)(resources.GetObject("control_XRayTable1.List_ShowElementInfo")));
  591. this.control_XRayTable1.Location = new System.Drawing.Point(0, 0);
  592. this.control_XRayTable1.Margin = new System.Windows.Forms.Padding(2);
  593. this.control_XRayTable1.Name = "control_XRayTable1";
  594. this.control_XRayTable1.ShowAnalysisXray = true;
  595. this.control_XRayTable1.ShowSearchXray = false;
  596. this.control_XRayTable1.Size = new System.Drawing.Size(781, 200);
  597. this.control_XRayTable1.STDName = "";
  598. this.control_XRayTable1.TabIndex = 0;
  599. this.control_XRayTable1 = new OTSIncAGraph.Controls.Control_XRayTable();
  600. this.panelXray.Controls.Add(this.control_XRayTable1);
  601. //添加鼠标点击事件
  602. myChart.MouseClick += new MouseEventHandler(this.LZMeasureStatusWindow_MouseClick);
  603. picBox.MouseClick += new MouseEventHandler(HiddenXrayTablePanel_MouseClick);
  604. //
  605. // pbBSEImage添加鼠标操作
  606. //
  607. this.picBox.MouseUp += new MouseEventHandler(this.pbBSEImage_MouseUp);
  608. this.picBox.MouseDown += new MouseEventHandler(this.pbBSEImage_MouseDown);
  609. this.MouseUp += new MouseEventHandler(this.pbBSEImage_MouseUp);
  610. this.MouseDown += new MouseEventHandler(this.pbBSEImage_MouseDown);
  611. this.MouseWheel += new MouseEventHandler(this.pbBSEImage_MouseWheel);
  612. log = NLog.LogManager.GetCurrentClassLogger();
  613. }
  614. private void HiddenXrayTablePanel_MouseClick(object sender, MouseEventArgs e)
  615. {
  616. if (e.Button != MouseButtons.Right)
  617. {
  618. if (!IsShowGrayLevelLine)
  619. {
  620. if (m_ElementPointGDIObjects != null)
  621. {
  622. if (m_ElementPointGDIObjects.Count > 0)
  623. {
  624. foreach (var item in m_ElementPointGDIObjects)
  625. {
  626. if (Math.Abs(e.Y - item.GetOrigionalDrawRegionF().Y) < 5 && Math.Abs(e.X - item.GetOrigionalDrawRegionF().X) < 5)
  627. {
  628. //显示Xray图像
  629. panelXray.Visible = true;
  630. }
  631. else
  632. {
  633. panelXray.Visible = false;
  634. }
  635. }
  636. }
  637. }
  638. }
  639. }
  640. }
  641. private void LZMeasureStatusWindow_MouseClick(object sender, MouseEventArgs e)
  642. {
  643. if (e.Button == MouseButtons.Right && e.Clicks == 1)
  644. {
  645. contextMenuStrip1.Show(PointToScreen(e.Location));
  646. SetContentMenu();
  647. }
  648. }
  649. #region 设置右键菜单中的项 是否可编辑
  650. protected void SetContextMenuEnabled(bool isEnabled)
  651. {
  652. //隐藏右键菜单中的项
  653. //MenuItemLineScan.Checked = false;
  654. MenuItemLineScan.Enabled = true;
  655. PointScanElementMenuItem.Enabled = isEnabled;
  656. bool showExprot = m_ElementPointGDIObjects.Count > 0 ? true : false;
  657. ExportScanInfoMenuItem.Enabled = showExprot;
  658. IsShowGrayLevelLine = false;
  659. VisualAdjustingMenu.Enabled = isEnabled;
  660. toolStripMenuItem1.Enabled = true;
  661. this.Cursor = Cursors.Default;
  662. this.Validate();
  663. }
  664. #endregion
  665. #region 获取行数据
  666. private void GetLineImage(int iHeight, int iWidth, ref byte[] bdata)
  667. {
  668. Stream stream = null;
  669. if (m_IsShowImageType == ImageType.BSEImage)
  670. {
  671. stream = new MemoryStream(originalBseData);
  672. }
  673. else if (m_IsShowImageType == ImageType.RemoveBGImage)
  674. {
  675. stream = new MemoryStream(removeBGBseData);
  676. }
  677. if (iHeight >= 0)
  678. {
  679. stream.Seek(iHeight * iWidth, SeekOrigin.Begin);
  680. }
  681. stream.Read(bdata, 0, iWidth);
  682. }
  683. #endregion
  684. private void panel1_Click(object sender, EventArgs e)
  685. {
  686. picBox.Show();
  687. }
  688. private void contextMenuStrip1_Opened(object sender, EventArgs e)
  689. {
  690. if (bfResult)
  691. {
  692. SampleParaLock.Enabled = true;
  693. ToolStripMenuItem.Enabled = true;
  694. grayToolStripMenuItem.Enabled = true;
  695. AbandonGrayStripMenuItem.Enabled = true;
  696. toolStripMenuItem2.Enabled = true;
  697. ChangeDiffImageShow.Enabled = true;
  698. if (0 <= m_MouseMoveImageX && m_MouseMoveImageX <= 1024)
  699. {
  700. PointScanElementMenuItem.Enabled = true;
  701. }
  702. else
  703. {
  704. PointScanElementMenuItem.Enabled = false;
  705. }
  706. if (!MenuItemLineScan.Checked)
  707. {
  708. PointScanElementMenuItem.Enabled = true;
  709. bool showExprot = m_ElementPointGDIObjects.Count > 0 ? true : false;
  710. if (showExprot)
  711. {
  712. ExportScanInfoMenuItem.Enabled = true;
  713. }
  714. }
  715. else
  716. {
  717. PointScanElementMenuItem.Enabled = false;
  718. ExportScanInfoMenuItem.Enabled = false;
  719. return;
  720. }
  721. if (myChart.Visible)
  722. {
  723. //MenuItemLineScan.Enabled = false;
  724. PointScanElementMenuItem.Enabled = false;
  725. ExportScanInfoMenuItem.Enabled = false;
  726. }
  727. else
  728. {
  729. //MenuItemLineScan.Enabled = true;
  730. PointScanElementMenuItem.Enabled = true;
  731. bool showExprot = m_ElementPointGDIObjects.Count > 0 ? true : false;
  732. if (showExprot)
  733. {
  734. ExportScanInfoMenuItem.Enabled = true;
  735. }
  736. }
  737. //根据去背景方式 设置可视化使用状态
  738. COTSSample WSample = m_MeasureAppForm.m_ProjData.GetWorkingSample();
  739. COTSImageProcParam ImgProcPrm = WSample.GetMsrParams().GetImageProcessParam();
  740. if (ImgProcPrm.GetBGRemoveType() == otsdataconst.OTS_BGREMOVE_TYPE.MANUAL)
  741. {
  742. VisualAdjustingMenu.Enabled = true;
  743. }
  744. else
  745. {
  746. VisualAdjustingMenu.Enabled = false;
  747. }
  748. }
  749. else
  750. {
  751. ToolStripMenuItem.Enabled = false;
  752. grayToolStripMenuItem.Enabled = false;
  753. MenuItemLineScan.Enabled = false;
  754. AbandonGrayStripMenuItem.Enabled = false;
  755. }
  756. }
  757. //拖动灰度线
  758. #region 拖动灰度线
  759. int LineX = 0;
  760. int LineXEnd = 0;
  761. public Point pointStart = new Point(100, 100);
  762. public Point pointEnd = new Point(400, 100);
  763. private bool m_bMouseDown = false;
  764. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  765. {
  766. if (bfResult)
  767. {
  768. if (IsShowGrayLevelLine)
  769. {
  770. Rectangle rectangle = GetPictureBoxZoomSize(picBox);
  771. if (pointStart.Equals(pointEnd))
  772. {
  773. return;
  774. }
  775. Point start = new Point(100, pointStart.Y);
  776. Point end = new Point(500, pointEnd.Y);
  777. double rate = (double)picBox.Image.Height / (double)rectangle.Height;
  778. int YSize = Convert.ToInt32((rate * (double)pointStart.Y));
  779. //显示线扫描 黄色线的曲线数据
  780. var Imagedata = new byte[m_iWidth];
  781. GetYSize();
  782. GetLineImage(YSize, m_iWidth, ref Imagedata);
  783. DrawCurve(Imagedata, e);
  784. e.Graphics.DrawLine(new Pen(Color.Red), new Point(rectangle.X, rectangle.Y + (rectangle.Height) / 4), new Point(rectangle.X + rectangle.Width, rectangle.Y + (rectangle.Height) / 4));
  785. e.Graphics.DrawLine(new Pen(Color.Red), new Point(rectangle.X, rectangle.Y + rectangle.Height * 3 / 4), new Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height * 3 / 4));
  786. e.Graphics.DrawLine(new Pen(Color.Blue), new Point(rectangle.X, pointStart.Y), new Point(rectangle.X + rectangle.Width, pointEnd.Y));
  787. #region 绘制刻度条
  788. float offHeight = (rectangle.Y + (rectangle.Height) / 4) - (rectangle.Y + rectangle.Height * 3 / 4);
  789. float grayValue = Math.Abs(offHeight / 255);
  790. //设置文字对齐方式
  791. StringFormat sf = new StringFormat();
  792. sf.Alignment = StringAlignment.Center;
  793. sf.LineAlignment = StringAlignment.Center;
  794. SolidBrush solidBrush = new SolidBrush(Color.FromArgb(100, Color.Red));
  795. //字体大小 根据样品孔Rectangle大小
  796. float fontSize = 15;
  797. Font font = new Font("黑体", fontSize, FontStyle.Bold);
  798. Pen pen = new Pen(Color.Red, 1);
  799. pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
  800. pen.DashPattern = new float[] { 5, 5 };
  801. int fontOffWidth = 50;
  802. int fontOffHeight = 10;
  803. int scaleLine = 0;
  804. for (int i = 25; i <= 225; i += 25)
  805. {
  806. scaleLine = (int)(grayValue * i);
  807. e.Graphics.DrawLine(pen, new Point(rectangle.X, (rectangle.Y + rectangle.Height * 3 / 4) - scaleLine), new Point(rectangle.X + fontOffWidth, (rectangle.Y + rectangle.Height * 3 / 4) - scaleLine));
  808. e.Graphics.DrawString(i.ToString(), font, solidBrush, rectangle.X + 20, (rectangle.Y + rectangle.Height * 3 / 4) - scaleLine - fontOffHeight, sf);
  809. }
  810. #endregion
  811. }
  812. }
  813. if (!MenuItemLineScan.Checked)
  814. {
  815. if (m_ElementPointGDIObjects != null)
  816. {
  817. if (m_ElementPointGDIObjects.Count > 0)
  818. {
  819. //绘制采集点标记
  820. foreach (CRectangleGDIObject item in m_ElementPointGDIObjects)
  821. {
  822. if (item != null)
  823. {
  824. item.OnPaint(e);
  825. }
  826. }
  827. }
  828. }
  829. }
  830. }
  831. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  832. {
  833. //如果pictureBox1中没有图像 则return退出
  834. if (picBox.Image == null)
  835. {
  836. return;
  837. }
  838. Control lCtrl = (sender as Control);
  839. Rectangle rectangle = GetPictureBoxZoomSize(picBox);
  840. int startimageX = 0;
  841. int startimageY = 0;
  842. //处理灰度值
  843. int grayValue = 0;
  844. // 生成鼠标的位置
  845. int MouseLine = e.X;
  846. //当前鼠标所在图片中的位置
  847. int imageX = 0;
  848. int imageY = 0;
  849. //当前pictrueBox与实际图片的X、Y比例
  850. double rateX = 0;
  851. double rateY = 0;
  852. if (originalBseData != null)
  853. {
  854. if ((picBox.Width - rectangle.Width) / 2 < e.X && e.X < (picBox.Width - rectangle.Width) / 2 + rectangle.Width)
  855. {
  856. imageX = e.X - (picBox.Width - rectangle.Width) / 2;
  857. rateX = (double)m_iWidth / (double)rectangle.Width;
  858. startimageX = Convert.ToInt32(imageX * rateX);
  859. m_MouseMoveImageX = startimageX;
  860. if (MoveFlag)
  861. {
  862. picBox.Left += Convert.ToInt32(e.X - xPos);//设置x坐标.
  863. picBox.Top += Convert.ToInt32(e.Y - yPos);//设置y坐标.
  864. }
  865. }
  866. else
  867. {
  868. startimageX = 0;
  869. m_MouseMoveImageX = -1;
  870. }
  871. if ((picBox.Height - rectangle.Height) / 2 < e.Y && e.Y < (picBox.Height - rectangle.Height) / 2 + rectangle.Height)
  872. {
  873. imageY = e.Y - (picBox.Height - rectangle.Height) / 2;
  874. rateY = (double)m_iHeight / (double)rectangle.Height;
  875. startimageY = Convert.ToInt32(imageY * rateY);
  876. m_MouseMoveImageY = startimageY;
  877. }
  878. //如果当前鼠标在图像Y轴中
  879. if (imageX > 0 && imageY > 0)
  880. {
  881. if (imageY > 1)
  882. {
  883. startimageX = startimageX + (startimageY * m_iWidth);
  884. if (m_IsShowImageType == ImageType.BSEImage)
  885. {
  886. grayValue = originalBseData[startimageX];
  887. }
  888. else
  889. {
  890. grayValue = removeBGBseData[startimageX];
  891. }
  892. }
  893. else if (startimageY == 1)
  894. {
  895. if (m_IsShowImageType == ImageType.BSEImage)
  896. {
  897. grayValue = originalBseData[startimageX];
  898. }
  899. else
  900. {
  901. grayValue = removeBGBseData[startimageX];
  902. }
  903. }
  904. }
  905. else
  906. {
  907. grayValue = 0;
  908. }
  909. }
  910. LineX = rectangle.X + 1;
  911. LineXEnd = rectangle.X + rectangle.Width;
  912. if (LineX < MouseLine && MouseLine < LineXEnd)
  913. {
  914. if (MenuItemLineScan.Checked)
  915. {
  916. if (Math.Abs(e.Y - pointStart.Y) < 2)
  917. {
  918. this.Cursor = Cursors.SizeNS;
  919. }
  920. else
  921. {
  922. this.Cursor = Cursors.Default;
  923. }
  924. }
  925. }
  926. //Y值
  927. double rate = (double)picBox.Image.Height / (double)rectangle.Height;
  928. int YSize = Convert.ToInt32((rate * (double)e.Y));
  929. string strGrayValue = grayValue.ToString();
  930. switch (strGrayValue.ToString().Length)
  931. {
  932. case 1:
  933. strGrayValue = " " + grayValue.ToString();
  934. break;
  935. case 2:
  936. strGrayValue = " " + grayValue.ToString();
  937. break;
  938. }
  939. //此处调接口显示灰度值
  940. m_MeasureAppForm.ShowGrayVal(strGrayValue.ToString());
  941. if (!IsShowGrayLevelLine)
  942. {
  943. if (m_ElementPointGDIObjects != null)
  944. {
  945. if (m_ElementPointGDIObjects.Count > 0)
  946. {
  947. foreach (var item in m_ElementPointGDIObjects)
  948. {
  949. Rectangle rect = new Rectangle(e.Location, new Size(1, 1));
  950. if (Math.Abs(e.Y - item.GetOrigionalDrawRegionF().Y) < 5 && Math.Abs(e.X - item.GetOrigionalDrawRegionF().X) < 5)
  951. {
  952. //设置鼠标样式
  953. this.Cursor = Cursors.Hand;
  954. }
  955. else
  956. {
  957. this.Cursor = Cursors.Default;
  958. }
  959. }
  960. }
  961. }
  962. }
  963. if (e.Button != MouseButtons.Left)
  964. {
  965. return;
  966. }
  967. pointStart = new Point(pointStart.X, e.Y);
  968. pointEnd = new Point(pointEnd.X, e.Y);
  969. picBox.Refresh();
  970. }
  971. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  972. {
  973. m_mouseCurrentPoint = e.Location;
  974. if (e.Button != MouseButtons.Left)
  975. {
  976. return;
  977. }
  978. m_bMouseDown = !m_bMouseDown;
  979. }
  980. #endregion
  981. //显示去背景BSE图得灰度曲线值
  982. #region 显示去背景BSE图得灰度曲线值
  983. private void ShowAbandonGrayLevelImage_Click(object sender, EventArgs e)
  984. {
  985. //设置右键菜单中的项 是否可编辑
  986. SetContextMenuEnabled(false);
  987. ShowAbandonGrayLevelImage();
  988. myChart.Visible = true;
  989. panelXray.Visible = false;
  990. IsShowGrayLevelLine = false;
  991. }
  992. private double[] GetGrayAbandonData(byte[] Imagedata, double[] GrayAbandonLevelData)
  993. {
  994. //获得下标每一点的灰度值,并在数组里加一
  995. for (int i = 0; i < Imagedata.Length; i++)
  996. {
  997. byte graylevel = Imagedata[i];
  998. if (graylevel > 0 && graylevel < 255)
  999. {
  1000. GrayAbandonLevelData[graylevel] += 1;
  1001. }
  1002. }
  1003. return GrayAbandonLevelData;
  1004. }
  1005. protected void ShowAbandonGrayLevelImage()
  1006. {
  1007. m_imagetype = ImageType.GrayAbandonLevelChart;
  1008. COTSSample WSample = m_MeasureAppForm.m_ProjData.GetWorkingSample();
  1009. var pixelSize = WSample.CalculatePixelSize();
  1010. var imageHandle = new CImageHandler();
  1011. bfResult = imageHandle.GetBSEImage(WSample.GetMsrParams().GetImageProcessParam(), pixelSize, originalBseData, m_iWidth, m_iHeight, ref removeBGBseData);
  1012. if (bfResult)
  1013. {
  1014. dGrayAbandonLevelData = new double[(int)GrayLevel.Max];
  1015. GetGrayAbandonData(removeBGBseData, dGrayAbandonLevelData);
  1016. //计算灰度去背景最大值
  1017. dGrayAbandonLevelMax = dGrayAbandonLevelData[0];
  1018. for (int i = 0; i < (int)GrayLevel.Max; i++)
  1019. {
  1020. dGrayAbandonLevelMax = Math.Max(dGrayAbandonLevelMax, dGrayAbandonLevelData[i]);
  1021. }
  1022. }
  1023. picBox.Hide();
  1024. int[] XData = new int[(int)GrayLevel.Max];
  1025. for (int i = 0; i < (int)GrayLevel.Max; i++)
  1026. {
  1027. XData[i] = i + 1;
  1028. }
  1029. int[] YData = new int[dGrayAbandonLevelData.Length];
  1030. for (int i = 0; i < dGrayAbandonLevelData.Length; i++)
  1031. {
  1032. YData[i] = (int)dGrayAbandonLevelData[i];
  1033. }
  1034. chartData(XData, YData);
  1035. }
  1036. #endregion
  1037. private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
  1038. {
  1039. if (e.Button == MouseButtons.Right && e.Clicks == 1)
  1040. {
  1041. int XMenu = picBox.Left + e.Location.X;
  1042. int YMenu = picBox.Top + e.Location.Y;
  1043. Point mousePoint = new Point(XMenu, YMenu);
  1044. contextMenuStrip1.Show(PointToScreen(mousePoint));
  1045. //电镜位置信息
  1046. Point semLocatin = new Point((int)dPositionX, (int)dPositionY);
  1047. //Rectangle semRect = new Rectangle();
  1048. if (IsSEMLocationOnTheSampleStage(semLocatin))
  1049. {
  1050. SetContentMenu();
  1051. }
  1052. }
  1053. }
  1054. private void ChangeDiffImageShow_Click(object sender, EventArgs e)
  1055. {
  1056. SetContextMenuEnabled(true);
  1057. //显示BSE图
  1058. if (originalBseData != null)
  1059. {
  1060. //用于线扫描曲线
  1061. m_IsShowImageType = ImageType.BSEImage;
  1062. m_imagetype = ImageType.BSEImage;
  1063. //在控件中显示图像
  1064. ShowImage(m_iWidth, m_iHeight, originalBseData);
  1065. picBox.Show();
  1066. }
  1067. m_CurrentMultiple = 0;
  1068. panelXray.Visible = false;
  1069. }
  1070. #region 显示鼠标位置
  1071. /// <summary>
  1072. /// 显示鼠标位置
  1073. /// </summary>
  1074. /// <param name="eMouseLocation">当前处理后的鼠标路径 减去边缘</param>
  1075. /// <param name="imageRect">Pictrue中的图像尺寸与位置</param>
  1076. public void ShowMouseSEMCurrentLocation(Point eMouseLocation, Rectangle imageRect, Rectangle semRect)
  1077. {
  1078. //真实图像中心点
  1079. Point imgRealCenterPoint = new Point();
  1080. //显示图像中心点
  1081. Point imgCenterPoint = new Point();
  1082. int imgWidth = 0;
  1083. int imgHeight = 0;
  1084. //获取真实图片的分辨率
  1085. m_MeasureAppForm.m_ProjParam.GetBSEImageSize(ref imgWidth, ref imgHeight);
  1086. //根据真实图像的像素 获取真实图像中心点
  1087. imgRealCenterPoint.X = imgWidth / 2;
  1088. imgRealCenterPoint.Y = imgHeight / 2;
  1089. //图像与真是图像的宽高的比例
  1090. double witdhRote = 0;
  1091. double heightRote = 0;
  1092. //获取显示图像与真是图像的宽高的比例
  1093. witdhRote = (double)imageRect.Width / imgWidth;
  1094. heightRote = (double)imageRect.Height / imgHeight;
  1095. //获取每个单位中包含多少像素 根据帧图的长度 与 真实图像的长度计算
  1096. double lenghtInPixWidth = (double)FieldWitdh / imgWidth / witdhRote;
  1097. double lenghtInPixHeight = (double)FieldHeight / imgHeight / heightRote;
  1098. //通过鼠标当前位置 获取位置 原点为控件的左上角起始点
  1099. double MouseWidth = (double)eMouseLocation.X * lenghtInPixWidth;
  1100. double MouseHeight = (double)eMouseLocation.Y * lenghtInPixHeight;
  1101. //根据显示图像的像素 获取真实图像中心点
  1102. imgCenterPoint.X = imageRect.Width / 2;
  1103. imgCenterPoint.Y = imageRect.Height / 2;
  1104. //通过鼠标当前位置 获取位置 原点为控件的左上角起始点
  1105. double imgCenterWidth = (double)imgCenterPoint.X * lenghtInPixWidth;
  1106. double imgCenterHeight = (double)imgCenterPoint.Y * lenghtInPixHeight;
  1107. //获取当前鼠标坐标与SEM当前坐标的距离 单位毫米
  1108. double apartCenterPointWidth = MouseWidth - imgCenterWidth;
  1109. double apartCenterPointHeight = MouseHeight - imgCenterHeight;
  1110. //将微米转换为毫米
  1111. float mousePointX = Convert.ToSingle((apartCenterPointWidth / 1000).ToString("F3"));
  1112. float mousePointY = Convert.ToSingle((apartCenterPointHeight / 1000).ToString("F3"));
  1113. //根据Sem电镜当前位置 计算与SEM坐标起始点相差的距离
  1114. //1.获取电镜当前位置
  1115. double semX = dPositionX;
  1116. double semY = dPositionY;
  1117. //2.获取当前电镜位置与SEM原点位置的距离
  1118. string apartToSEMWidth = Convert.ToSingle(semX + mousePointX).ToString("F3");
  1119. string apartToSEMHeight = Convert.ToSingle(semY - mousePointY).ToString("F3");
  1120. //编辑显示内容
  1121. string STSemCoordinate = "X:" + apartToSEMWidth + "|Y:" + apartToSEMHeight + "";
  1122. }
  1123. #endregion
  1124. #region 设置测量状态初始化
  1125. /// <summary>
  1126. /// 设置测量状态初始化
  1127. /// </summary>
  1128. public void SetMeasureStatusInit()
  1129. {
  1130. picBox.Hide();
  1131. foreach (ToolStripItem item in contextMenuStrip1.Items)
  1132. {
  1133. if (item != contextMenuStrip1.Items[(int)MeasureStateMenuType.ReadBSEPic])
  1134. {
  1135. item.Enabled = false;
  1136. }
  1137. }
  1138. }
  1139. #endregion
  1140. #region 鼠标是否在图像上移动
  1141. /// <summary>
  1142. /// 鼠标是否在图像上移动
  1143. /// </summary>
  1144. /// <param name="mouseCurrentLocation"></param>
  1145. /// <param name="picBox"></param>
  1146. /// <returns></returns>
  1147. public bool IsOnTheImage(Point mouseCurrentLocation, PictureBox picBox, ref Point mouseLocation, ref Rectangle imageRect)
  1148. {
  1149. //通过Pictrue获取图像尺寸位置
  1150. Rectangle rectangle = GetPictureBoxZoomSize(picBox);
  1151. imageRect = rectangle;
  1152. //当前鼠标坐标是否包含在图像中
  1153. bool result = imageRect.Contains(mouseCurrentLocation);
  1154. if (result)
  1155. {
  1156. //获取与边框的间隔
  1157. mouseLocation.X = mouseCurrentLocation.X - rectangle.X;
  1158. mouseLocation.Y = mouseCurrentLocation.Y;
  1159. return true;
  1160. }
  1161. return false;
  1162. }
  1163. #endregion
  1164. #region 判断当前电镜位置是否在样品台内部
  1165. /// <summary>
  1166. /// 判断当前电镜位置是否在样品台内部
  1167. /// </summary>
  1168. /// <param name="semLocation">电镜当前位置</param>
  1169. /// <param name="SampleStage">样品台尺寸与位置</param>
  1170. /// <returns></returns>
  1171. public bool IsSEMLocationOnTheSampleStage(Point semLocation)
  1172. {
  1173. return m_MeasureAppForm.m_SamplepaceWindow.m_visualStage.IfMouseInStage(semLocation);
  1174. }
  1175. #endregion
  1176. #region 设置右键菜单项是否可用
  1177. /// <summary>
  1178. /// 设置右键菜单项是否可用
  1179. /// </summary>
  1180. public void SetContentMenu()
  1181. {
  1182. int sampleCount = m_MeasureAppForm.m_ProjParam.GetSampleCount();
  1183. if (sampleCount == 0)
  1184. {
  1185. foreach (ToolStripItem item in contextMenuStrip1.Items)
  1186. {
  1187. item.Enabled = false;
  1188. }
  1189. }
  1190. else
  1191. {
  1192. if (m_MeasureAppForm.MeasureThreadRunFlag)
  1193. {
  1194. if (m_MeasureAppForm.m_MsrThreadWrapper.BgWorkIsPaused())
  1195. {
  1196. foreach (ToolStripItem item in contextMenuStrip1.Items)
  1197. {
  1198. item.Enabled = true;
  1199. }
  1200. }
  1201. else
  1202. {
  1203. foreach (ToolStripItem item in contextMenuStrip1.Items)
  1204. {
  1205. item.Enabled = false;
  1206. }
  1207. }
  1208. }
  1209. double iWDistance = 0;
  1210. double dMagni = 0;
  1211. m_MeasureAppForm.m_ProjParam.GetWorkingSampleSEMData(ref iWDistance, ref dMagni);
  1212. if (iWDistance != 0 && dMagni != 0)
  1213. {
  1214. contextMenuStrip1.Items[(int)MeasureStateMenuType.ReadBSEPic].Enabled = true;
  1215. }
  1216. }
  1217. }
  1218. #endregion
  1219. #region 获取线扫描初始位置
  1220. public void GetYSize()
  1221. {
  1222. Rectangle rectangle = GetPictureBoxZoomSize(picBox);
  1223. double rate = (double)picBox.Image.Height / (double)rectangle.Height;
  1224. YSize = Convert.ToInt32((rate * (double)pointStart.Y));
  1225. }
  1226. #endregion
  1227. public void chartData(int[] XData, int[] YData)
  1228. {
  1229. //添加数据
  1230. myChart.Series[0].Points.DataBindXY(XData, YData);
  1231. myChart.Series[0].Label = "";
  1232. myChart.Series[0].LegendText = "";
  1233. myChart.Series[0].Name = "Graph";
  1234. myChart.Series[0].IsVisibleInLegend = false;
  1235. switch (m_imagetype)
  1236. {
  1237. case ImageType.GrayLevelChart:
  1238. myChart.Titles[0].Text = "B-GrayCurve";
  1239. break;
  1240. case ImageType.GrayAbandonLevelChart:
  1241. myChart.Titles[0].Text = "Remove Background Gray-scaleCurve";
  1242. break;
  1243. default:
  1244. myChart.Titles[0].Text = "";
  1245. break;
  1246. }
  1247. }
  1248. private void PointScanElementMenuItem_Click(object sender, EventArgs e)
  1249. {
  1250. MenuItemLineScan.Checked = false;
  1251. //电镜设置对象
  1252. //控制类对象初始化
  1253. var expC = m_MeasureAppForm.m_ProjData.GetWorkingSample().GetMsrParams().GetXRayParam().GetAnalyExpCount();
  1254. IEDSController m_EDSHardwareMgr = EDSController.GetEDSController(m_iWidth, m_iHeight, expC);
  1255. //SemController m_SEMHardwareMgr = SemController.GetSEMController();
  1256. //1.连接电镜
  1257. if (m_EDSHardwareMgr.Connect())
  1258. {
  1259. COTSMsrPrjResultData cProjMgr = m_MeasureAppForm.m_ProjParam.GetResultData();
  1260. //获取工作样品对象
  1261. COTSSample sampleClr = cProjMgr.GetWorkingSample();
  1262. //清空之前记录的标识
  1263. if (m_ElementPointGDIObjects != null)
  1264. {
  1265. if (m_ElementPointGDIObjects.Count > 0)
  1266. {
  1267. //清空
  1268. m_ElementPointGDIObjects.Clear();
  1269. }
  1270. }
  1271. //2.获取鼠标在图像的位置
  1272. dMouseImgX = m_MouseMoveImageX;
  1273. dMouseImgY = m_MouseMoveImageY;
  1274. //3.根据所要扫描的位置进行元素分析
  1275. uint a_Milliseconds = 0;
  1276. //采集时间
  1277. a_Milliseconds = Convert.ToUInt32(sampleClr.GetMsrParams().GetXRayParam().GetMidAnalyAQTime());
  1278. //元素数量
  1279. ValueType a_nElementNum = 0;
  1280. //元素名称
  1281. string a_strElementName = "";
  1282. //元素Xray数据
  1283. uint[] a_ElementXrayData = new uint[2000];
  1284. //设置分辨率
  1285. //m_EDSHardwareMgr.SetResolution(m_iWidth, m_iHeight);
  1286. //设置期望计数率
  1287. //m_EDSHardwareMgr.SetAnalyExpCount();
  1288. if (GetXRayAndElements(a_Milliseconds, (int)dMouseImgX, (int)dMouseImgY, ref a_ElementXrayData, ref a_nElementNum, ref a_strElementName))
  1289. {
  1290. log.Trace("--获取单点扫描数据:元素数量:'" + a_nElementNum + "' ,元素名称:'" + a_strElementName + "'--");
  1291. //4.在BSE上绘制扫描标记
  1292. //4-1 获取鼠标在BSE图上的位置
  1293. Point p_mouseCurrentTempPoint = m_mouseCurrentPoint;
  1294. //4-2 根据位置绘制标记
  1295. Point ImgPoint = new Point((int)dMouseImgX, (int)dMouseImgY);
  1296. Rectangle rect = new Rectangle(p_mouseCurrentTempPoint, new Size(1, 1));
  1297. CRectangleGDIObject createRect = new CRectangleGDIObject(rect, ImgPoint, m_CurrentMultiple, CreateRectangleType.DrawBSEElementSignPoint);
  1298. m_ElementPointGDIObjects.Add(createRect);
  1299. //5.获取成功后弹出对话框
  1300. //计数率
  1301. double electronicCount = 0;
  1302. uint[] u_ElementXrayData = new uint[2000];
  1303. for (int i = 0; i < a_ElementXrayData.Length; i++)
  1304. {
  1305. u_ElementXrayData[i] = (uint)a_ElementXrayData[i];
  1306. //累加计数率
  1307. electronicCount += a_ElementXrayData[i];
  1308. }
  1309. //显示Xray控件
  1310. panelXray.Visible = true;
  1311. //获取数据后,需要对xraytable设置
  1312. string[] strGroup = a_strElementName.Split('\n');
  1313. //get CElementChemistryClr list
  1314. List<ShowElementInfo> list_showelementinfo = new List<ShowElementInfo>();
  1315. for (int i = 0; i < strGroup.Length; i++)
  1316. {
  1317. if (!strGroup[i].Equals(""))
  1318. {
  1319. ShowElementInfo ls_sei = new ShowElementInfo();
  1320. ls_sei.ElementName = strGroup[i].Split(':')[0];
  1321. ls_sei.Percentage = Convert.ToDouble(strGroup[i].Split(':')[1]);
  1322. list_showelementinfo.Add(ls_sei);
  1323. }
  1324. }
  1325. //设置分析Xray数据
  1326. control_XRayTable1.SetXRayShowLineValue(new uint[2000], u_ElementXrayData, list_showelementinfo);
  1327. //需要显示计数率
  1328. string strElectronicCount = string.Empty;
  1329. if (electronicCount > 1000)
  1330. {
  1331. strElectronicCount = (Convert.ToDouble(electronicCount) / 1000).ToString("f2") + "kcps";
  1332. }
  1333. control_XRayTable1.List_ShowElementInfo = list_showelementinfo;
  1334. }
  1335. ExportScanInfoMenuItem.Enabled = true;
  1336. this.Invalidate();
  1337. }
  1338. }
  1339. public bool GetXRayAndElements(uint a_nXRayAQTime, int dMouseImgX, int dMouseImgY, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  1340. {
  1341. var expC = m_MeasureAppForm.m_ProjData.GetWorkingSample().GetMsrParams().GetXRayParam().GetAnalyExpCount();
  1342. var eds = EDSController.GetEDSController(m_iWidth,m_iHeight,expC);
  1343. var mode = m_MeasureAppForm.m_ProjData.GetWorkingSample().GetMsrParams().GetXRayParam().GetScanMode();
  1344. var part = new COTSParticleClr();
  1345. part.SetXRayPos(new Point(dMouseImgX, dMouseImgY));
  1346. var xray = part.GetXray();
  1347. xray.SetIndex(0);
  1348. part.SetXray(xray);
  1349. var parts = new List<COTSParticleClr>();
  1350. parts.Add(part);
  1351. if (mode == otsdataconst.OTS_X_RAY_SCAN_MODE.PointMode)
  1352. {
  1353. eds.GetXRayByParts(parts, a_nXRayAQTime, true);
  1354. }
  1355. else
  1356. {
  1357. eds.GetXRayByFeatures(parts, a_nXRayAQTime, true);
  1358. }
  1359. a_XrayData = parts[0].GetXray().GetXrayData();
  1360. a_strResult = parts[0].GetXray().GetQuantifiedElementsStr();
  1361. return true;
  1362. }
  1363. public void DelElementPointGDIObjects()
  1364. {
  1365. if (m_ElementPointGDIObjects != null)
  1366. {
  1367. if (m_ElementPointGDIObjects.Count > 0)
  1368. {
  1369. //删除点标记
  1370. m_ElementPointGDIObjects.Clear();
  1371. this.Invalidate();
  1372. }
  1373. }
  1374. }
  1375. private void ExportScanInfoMenuItem_Click(object sender, EventArgs e)
  1376. {
  1377. //导出图片到报告中
  1378. ExportReport();
  1379. }
  1380. /// <summary>
  1381. /// 导出Image到报告
  1382. /// </summary>
  1383. private bool ExportReport()
  1384. {
  1385. string startupPath = System.Windows.Forms.Application.StartupPath + "\\TempReport\\";
  1386. if (!Directory.Exists(startupPath))
  1387. {
  1388. Directory.CreateDirectory(startupPath);
  1389. }
  1390. //保存Xray图片
  1391. string ImgFilePath = startupPath + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
  1392. Bitmap bseBM = new Bitmap(this.picBox.Width, this.picBox.Height);
  1393. this.picBox.DrawToBitmap(bseBM, new Rectangle(0, 0, this.picBox.Width, this.picBox.Height));
  1394. //保存图片
  1395. bseBM.Save(ImgFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  1396. //释放资源
  1397. bseBM.Dispose();
  1398. //保存Xray图片
  1399. string ImgXrayFilePath = startupPath + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
  1400. int xrayWidth = control_XRayTable1.Width;
  1401. int xrayHeight = control_XRayTable1.Height;
  1402. Bitmap xrayBM = new Bitmap(xrayWidth, xrayHeight);
  1403. this.control_XRayTable1.DrawToBitmap(xrayBM, new Rectangle(0, 0, xrayWidth, xrayHeight));
  1404. //保存图片
  1405. xrayBM.Save(ImgXrayFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  1406. //释放资源
  1407. xrayBM.Dispose();
  1408. List<string> listFilePath = new List<string>();
  1409. listFilePath.Add(ImgFilePath);
  1410. listFilePath.Add(ImgXrayFilePath);
  1411. List<ShowElementInfo> list_ElementInfo = control_XRayTable1.List_ShowElementInfo;
  1412. //1.编辑信息
  1413. DataTable dt = new DataTable();
  1414. dt.Columns.Add("元素名称");
  1415. dt.Columns.Add("原子数");
  1416. dt.Columns.Add("质量百分比");
  1417. dt.Columns.Add("摩尔比");
  1418. foreach (var item in list_ElementInfo)
  1419. {
  1420. //获取摩尔比与原子数
  1421. CElementChemistry cElementChemistry = new CElementChemistry();
  1422. cElementChemistry.SetName(item.ElementName);
  1423. cElementChemistry.SetPercentage(item.Percentage);
  1424. double molarValue = Math.Round(cElementChemistry.GetMolarPercentage(), 2);
  1425. int atomNum = (int)CElement.GetAtomicNum(item.ElementName);
  1426. DataRow dr = dt.NewRow();
  1427. dr[0] = item.ElementName;
  1428. dr[1] = atomNum;
  1429. dr[2] = item.Percentage;
  1430. dr[3] = molarValue;
  1431. dt.Rows.Add(dr);
  1432. }
  1433. DataGridView dgv = new DataGridView();
  1434. dgv.DataSource = dt.DefaultView;
  1435. List<DataTable> list_ElementData = new List<DataTable>();
  1436. list_ElementData.Add(dt);
  1437. //将数据插入Excel中
  1438. return InsertDataToExcelTable(list_ElementData, startupPath, listFilePath);
  1439. }
  1440. public bool InsertDataToExcelTable(List<System.Data.DataTable> a_list_ElementData, string a_filePath, List<string> a_ImgFilePath)
  1441. {
  1442. var m_xe = new ExcelEdit();
  1443. m_xe.Create();
  1444. Microsoft.Office.Interop.Excel.Worksheet ws = m_xe.GetSheet("Sheet1");
  1445. m_xe.m_ws = ws;
  1446. string str_ExcelFilePath = a_filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
  1447. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[0], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0, 400, 300);
  1448. m_xe.m_ws.Shapes.AddPicture(a_ImgFilePath[1], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 320, 400, 100);
  1449. if (str_ExcelFilePath.IndexOf(":") < 0)
  1450. {
  1451. return false;
  1452. }
  1453. if (m_xe.m_app == null)
  1454. {
  1455. return false;
  1456. }
  1457. //数据列表 行数
  1458. int dataRow = 30;
  1459. //写入标题
  1460. foreach (var dgv in a_list_ElementData)
  1461. {
  1462. for (int i = 0; i < dgv.Columns.Count; i++)
  1463. {
  1464. m_xe.m_ws.Cells[dataRow + 1, i + 1] = dgv.Columns[i].ColumnName;
  1465. }
  1466. //写入数值
  1467. for (int r = 0; r < dgv.Rows.Count; r++)
  1468. {
  1469. for (int i = 0; i < dgv.Columns.Count; i++)
  1470. {
  1471. m_xe.m_ws.Cells[dataRow + r + 2, i + 1] = dgv.Rows[r].ItemArray[i];
  1472. }
  1473. System.Windows.Forms.Application.DoEvents();
  1474. }
  1475. }
  1476. //列宽自适应
  1477. m_xe.m_ws.Columns.EntireColumn.AutoFit();
  1478. if (str_ExcelFilePath != "")
  1479. {
  1480. try
  1481. {
  1482. m_xe.m_wb.Saved = true;
  1483. m_xe.m_wb.SaveCopyAs(str_ExcelFilePath);
  1484. }
  1485. catch (Exception ex)
  1486. {
  1487. log.Error(ex.ToString());
  1488. }
  1489. }
  1490. //关闭excel
  1491. m_xe.Close();
  1492. //强行销毁
  1493. GC.Collect();
  1494. //干掉进程里的EXCEL
  1495. foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
  1496. {
  1497. if (p.ProcessName == "EXCEL")
  1498. {
  1499. p.Kill();
  1500. }
  1501. }
  1502. //删除图片文件
  1503. foreach (string imgPath in a_ImgFilePath)
  1504. {
  1505. if (File.Exists(imgPath))
  1506. {
  1507. //如果存在则删除
  1508. File.Delete(imgPath);
  1509. }
  1510. }
  1511. System.Diagnostics.Process.Start(str_ExcelFilePath);
  1512. return true;
  1513. }
  1514. public void HideXRayTable()
  1515. {
  1516. panelXray.Visible = false;
  1517. //清空元素标记对象
  1518. if (m_ElementPointGDIObjects != null)
  1519. {
  1520. if (m_ElementPointGDIObjects.Count > 0)
  1521. {
  1522. m_ElementPointGDIObjects.Clear();
  1523. }
  1524. }
  1525. }
  1526. //在picturebox的鼠标按下事件里,记录三个变量.
  1527. int xPos;
  1528. int yPos;
  1529. bool MoveFlag = false;
  1530. private void pbBSEImage_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  1531. {
  1532. if (e.Button == MouseButtons.Left)
  1533. {
  1534. //在线扫描未选中的情况下 设置移动标识与获取当前鼠标位置
  1535. if (!(MenuItemLineScan.Checked))
  1536. {
  1537. MoveFlag = true;//已经按下.
  1538. xPos = e.X;//当前x坐标.
  1539. yPos = e.Y;//当前y坐标.
  1540. }
  1541. else
  1542. {
  1543. MoveFlag = false;
  1544. }
  1545. }
  1546. }
  1547. //在picturebox的鼠标按下事件里.
  1548. private void pbBSEImage_MouseUp(object sender, MouseEventArgs e)
  1549. {
  1550. MoveFlag = false;
  1551. }
  1552. //在picturebox鼠标移动
  1553. private void pbBSEImage_MouseMove(object sender, MouseEventArgs e)
  1554. {
  1555. if (MoveFlag)
  1556. {
  1557. picBox.Left = Convert.ToInt32(e.X - xPos);//设置x坐标.
  1558. picBox.Top = Convert.ToInt32(e.Y - yPos);//设置y坐标.
  1559. }
  1560. }
  1561. //在picturebox鼠标缩放
  1562. private void pbBSEImage_MouseWheel(object sender, MouseEventArgs e)
  1563. {
  1564. //鼠标滚动缩放 BSE图
  1565. if (e.Delta > 0)
  1566. {
  1567. if (m_CurrentMultiple < m_MaxMultipleRange)
  1568. {
  1569. m_CurrentMultiple += Convert.ToSingle(m_Multiple.ToString("f2"));
  1570. picBox.Width += Convert.ToInt32(m_iWidth * m_Multiple);
  1571. picBox.Height += Convert.ToInt32(m_iHeight * m_Multiple);
  1572. WheelElementPointGDIObjects(e.Location, m_CurrentMultiple);
  1573. }
  1574. }
  1575. else
  1576. {
  1577. m_CurrentMultiple -= Convert.ToSingle(m_Multiple.ToString("f2"));
  1578. if (m_CurrentMultiple > 0)
  1579. {
  1580. picBox.Width -= Convert.ToInt32(m_iWidth * m_Multiple);
  1581. picBox.Height -= Convert.ToInt32(m_iHeight * m_Multiple);
  1582. }
  1583. else
  1584. {
  1585. m_CurrentMultiple = 0;
  1586. picBox.Height = this.RestoreBounds.Height;
  1587. picBox.Width = this.RestoreBounds.Height * 4 / 3;
  1588. picBox.Left = 0;
  1589. picBox.Top = 0;
  1590. Rectangle rectangle = GetPictureBoxZoomSize(picBox);
  1591. Point sizeChangePoint = rectangle.Location;
  1592. //修改窗体宽度后,设置单点采集的位置
  1593. if (m_ElementPointGDIObjects != null)
  1594. {
  1595. if (m_ElementPointGDIObjects.Count > 0)
  1596. {
  1597. if (m_GetBSEImageInitPoint != sizeChangePoint)
  1598. {
  1599. int sizeChangeX = sizeChangePoint.X - m_GetBSEImageInitPoint.X;
  1600. int sizeChangeY = sizeChangePoint.Y - m_GetBSEImageInitPoint.Y;
  1601. RectangleF sizeChangeRectF = m_ElementPointGDIObjects[0].GetZoomedRegionF();
  1602. sizeChangeRectF.X = m_ElementPointGDIObjects[0].GetZoomedRegionF().X + sizeChangeX;
  1603. m_ElementPointGDIObjects[0].SetInitRegionF(sizeChangeRectF);
  1604. m_GetBSEImageInitPoint = sizeChangePoint;
  1605. }
  1606. }
  1607. }
  1608. }
  1609. WheelElementPointGDIObjects(e.Location, m_CurrentMultiple);
  1610. }
  1611. }
  1612. /// <summary>
  1613. /// 缩放时改变 单点标识位置
  1614. /// </summary>
  1615. /// <param name="m_CurrentMultiple"></param>
  1616. public void WheelElementPointGDIObjects(Point refPoint, float CurrentMultiple)
  1617. {
  1618. if (m_ElementPointGDIObjects.Count > 0)
  1619. {
  1620. foreach (CRectangleGDIObject item in m_ElementPointGDIObjects)
  1621. {
  1622. item.Zoom(refPoint, CurrentMultiple);
  1623. }
  1624. }
  1625. }
  1626. private void button1_Click(object sender, EventArgs e)
  1627. {
  1628. COTSSample cSample = m_MeasureAppForm.m_ProjParam.GetWorkSample();
  1629. int start = cSample.GetMsrParams().GetImageProcessParam().GetBGGray().GetStart();
  1630. int end = cSample.GetMsrParams().GetImageProcessParam().GetBGGray().GetEnd();
  1631. Image bInput = picBox.Image;
  1632. List<ColorMap> colorMapTemp = new List<ColorMap>();
  1633. for (int i = start; i <= end; i++)
  1634. {
  1635. ColorMap colorMap = new ColorMap();
  1636. string colorName = "#" + Color.FromArgb(i, i, i).Name.ToString();
  1637. colorMap.OldColor = ColorTranslator.FromHtml(colorName);
  1638. colorMap.NewColor = Color.Red;
  1639. colorMapTemp.Add(colorMap);
  1640. }
  1641. Bitmap outBitmap = new Bitmap(bInput);
  1642. //创建Graphics对象
  1643. Graphics g = Graphics.FromImage(outBitmap);
  1644. //生成的图像大小
  1645. int width = bInput.Width;
  1646. int height = bInput.Height;
  1647. //编辑被着急图像所要显示的位置
  1648. Rectangle DrawRect = new Rectangle(0, 0, 1024, 768);
  1649. //编辑输出画布中着色的位置
  1650. Rectangle ShowRect = new Rectangle(0, 0, 1024, 768);
  1651. ImageAttributes attr = new ImageAttributes();
  1652. attr.SetRemapTable(colorMapTemp.ToArray());
  1653. //从输入图像中截图至临时图像中
  1654. g.DrawImage(bInput, ShowRect, 0, 0, DrawRect.Width, DrawRect.Height, GraphicsUnit.Pixel, attr);
  1655. picBox.Image = outBitmap;
  1656. }
  1657. private void OTSMeasureStatusWindow_MouseClick(object sender, MouseEventArgs e)
  1658. {
  1659. if (e.Button == MouseButtons.Right && e.Clicks == 1)
  1660. {
  1661. //contextMenuStrip1.Show();
  1662. contextMenuStrip1.Show(PointToScreen(e.Location));
  1663. SetContentMenu();
  1664. }
  1665. }
  1666. private void ViewStripMenuItem_Click(object sender, EventArgs e)
  1667. {
  1668. if (m_iWidth == 0 || m_iHeight == 0)
  1669. {
  1670. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolutionStr();// 举例:1024X768
  1671. string[] sArray = str.Split('X');
  1672. if (sArray[0] != "" && sArray[1] != "")
  1673. {
  1674. m_iWidth = Convert.ToInt32(sArray[0]);
  1675. m_iHeight = Convert.ToInt32(sArray[1]);
  1676. }
  1677. }
  1678. ToolWindow toolWindow = new ToolWindow(m_MeasureAppForm, this);
  1679. int grayStart = 0;
  1680. int grayEnd = 0;
  1681. if (originalBseData != null)
  1682. {
  1683. Bitmap bitmap = CImageHandler.ToGrayBitmap(originalBseData, m_iWidth, m_iHeight);
  1684. toolWindow.BseImg = bitmap;
  1685. toolWindow.BBseData = originalBseData;
  1686. //获取去背景灰度值
  1687. grayStart = m_MeasureAppForm.m_ProjParam.GetWorkSample().GetMsrParams().GetImageProcessParam().GetBGGray().GetStart();
  1688. grayEnd = m_MeasureAppForm.m_ProjParam.GetWorkSample().GetMsrParams().GetImageProcessParam().GetBGGray().GetEnd();
  1689. //设置可视化中的属性
  1690. toolWindow.BseGrayMinValue = grayStart;
  1691. toolWindow.BseGrayMaxValue = grayEnd;
  1692. }
  1693. DialogResult dialogResult = toolWindow.ShowDialog();
  1694. if (dialogResult == DialogResult.Yes)
  1695. {
  1696. //获取去背景参数
  1697. grayStart = toolWindow.BseGrayMinValue;
  1698. grayEnd = toolWindow.BseGrayMaxValue;
  1699. //设置去背景参数
  1700. COTSImageProcParam cOTSImgProc ;
  1701. cOTSImgProc = m_MeasureAppForm.m_ProjParam.GetResultData().GetWorkingSample().GetMsrParams().GetImageProcessParam();
  1702. CIntRange cIntRange = new CIntRange();
  1703. cIntRange.SetStart(grayStart);
  1704. cIntRange.SetEnd(grayEnd);
  1705. cOTSImgProc.SetBGGray(cIntRange);
  1706. m_MeasureAppForm.m_ProjParam.GetResultData().GetWorkingSample().GetMsrParams().SetImageProcessParam(cOTSImgProc);
  1707. //更新属性窗口
  1708. m_MeasureAppForm.UpdatePropertyVal();
  1709. //确认后进行去背景
  1710. var m_systype = m_MeasureAppForm.m_ProjParam.GetResultData().SystemTypeId;
  1711. if (m_systype == otsdataconst.OTS_SysType_ID.CleannessA)
  1712. {
  1713. ShowColoredRemoveBGImage();
  1714. }
  1715. else
  1716. {
  1717. ShowRemoveBGImage();
  1718. }
  1719. }
  1720. }
  1721. private void toolStripMenuItem1_Click(object sender, EventArgs e)
  1722. {
  1723. if (m_iWidth == 0 || m_iHeight == 0)
  1724. {
  1725. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolutionStr();// 举例:1024X768
  1726. string[] sArray = str.Split('X');
  1727. if (sArray[0] != "" && sArray[1] != "")
  1728. {
  1729. m_iWidth = Convert.ToInt32(sArray[0]);
  1730. m_iHeight = Convert.ToInt32(sArray[1]);
  1731. }
  1732. }
  1733. SpecialParticleForm specialParticleForm = new SpecialParticleForm(CSpecialGrayRangeParam.GetParamFileFullName(), m_MeasureAppForm, originalBseData, m_iWidth, m_iHeight, this);
  1734. DialogResult dialogResult = specialParticleForm.ShowDialog();
  1735. }
  1736. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  1737. {
  1738. //设置右键菜单中的项 是否可编辑
  1739. SetContextMenuEnabled(true);
  1740. ShowColoredRemoveBGImage();
  1741. m_CurrentMultiple = 0;
  1742. panelXray.Visible = false;
  1743. }
  1744. /// <summary>
  1745. /// 调用AI http 服务获得二值图
  1746. /// </summary>
  1747. private void aiProcess()
  1748. {
  1749. //获取电镜中图像大小
  1750. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolutionStr();
  1751. string[] sArray = str.Split('X');
  1752. if (sArray[0] != "" && sArray[1] != "")
  1753. {
  1754. m_iWidth = Convert.ToInt32(sArray[0]);
  1755. m_iHeight = Convert.ToInt32(sArray[1]);
  1756. }
  1757. //获取图像数据
  1758. COTSSample WSample = m_MeasureAppForm.m_ProjData.GetWorkingSample();
  1759. double pixelSize = WSample.CalculatePixelSize();
  1760. var imageProcessParam = WSample.GetMsrParams().GetImageProcessParam();
  1761. var aiserver = imageProcessParam.GetAIServer();
  1762. OpenCvSharp.Mat input = new OpenCvSharp.Mat();
  1763. Bitmap bitmap = CImageHandler.ToGrayBitmap(originalBseData, m_iWidth, m_iHeight);
  1764. input = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);
  1765. //OpenCvSharp.Cv2.ImShow("img", input);
  1766. //OpenCvSharp.Cv2.WaitKey(0);
  1767. //OpenCvSharp.Cv2.DestroyAllWindows();
  1768. string baseurl = "http://" + aiserver;
  1769. //test server
  1770. bool test = false;
  1771. if(test)
  1772. {
  1773. if (!AI_HttpClient.AI_Test(baseurl))
  1774. {
  1775. log.Info("server erro!");
  1776. MessageBox.Show("server erro!");
  1777. input.Dispose();
  1778. return;
  1779. }
  1780. }
  1781. if (!input.Empty())
  1782. {
  1783. OpenCvSharp.Mat output = new OpenCvSharp.Mat();
  1784. AI_HttpClient.AI_SegformerImage_cxx(baseurl, input, ref output, 1007, 1);
  1785. if (output.Empty())
  1786. {
  1787. log.Info("aiserver out erro!");
  1788. MessageBox.Show("aiserver erro!");
  1789. input.Dispose();
  1790. output.Dispose();
  1791. return;
  1792. }
  1793. //output = output - 30;
  1794. output = ~output; //适配 ShowColoredRemoveBGImage_Ai
  1795. // setcoloredParticleImage(output, ref coloredParticleImage);
  1796. // ShowBitmap(m_iWidth, m_iHeight, coloredParticleImage);
  1797. removeBGBseData = CImageHandler.BitmapToGrayByte(output.ToBitmap());
  1798. //设置右键菜单中的项 是否可编辑
  1799. SetContextMenuEnabled(true);
  1800. ShowColoredRemoveBGImage_Ai(CImageHandler.BitmapToGrayByte(output.ToBitmap()));
  1801. m_CurrentMultiple = 0;
  1802. panelXray.Visible = false;
  1803. //OpenCvSharp.Cv2.ImShow("out", output);
  1804. //OpenCvSharp.Cv2.WaitKey(0);
  1805. //OpenCvSharp.Cv2.DestroyAllWindows();
  1806. input.Dispose();
  1807. output.Dispose();
  1808. IsShowGrayLevelLine = false;
  1809. myChart.Visible = false;
  1810. //切换显示至BSE图
  1811. contextMenuStrip1.Items[(int)MeasureStateMenuType.ChangeDiffImageShow].Enabled = true;
  1812. }
  1813. }
  1814. /// <summary>
  1815. /// 提取aiprocess result 连通域;找到颗粒
  1816. /// </summary>
  1817. /// <param name="output">aiprocess result</param>
  1818. /// <param name="coloredParticleImage"></param>
  1819. private void setcoloredParticleImage(OpenCvSharp.Mat output, ref Bitmap coloredParticleImage)
  1820. {
  1821. //提取连通域;用不同颜色显示连通域
  1822. OpenCvSharp.Mat labels = new OpenCvSharp.Mat(output.Size(), OpenCvSharp.MatType.CV_32S);
  1823. int numLabels = OpenCvSharp.Cv2.ConnectedComponents(output, labels, OpenCvSharp.PixelConnectivity.Connectivity8, OpenCvSharp.MatType.CV_32S);
  1824. //随机颜色
  1825. Random random = new Random();
  1826. OpenCvSharp.Mat coloredRegion = new OpenCvSharp.Mat(output.Size(), OpenCvSharp.MatType.CV_8UC3, OpenCvSharp.Scalar.All(0)); // 初始化为黑色
  1827. for (int i = 1; i < numLabels; i++)
  1828. {
  1829. // 生成随机颜色
  1830. OpenCvSharp.Scalar color = new OpenCvSharp.Scalar(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
  1831. // 提取当前连通区域的掩码
  1832. OpenCvSharp.Mat mask = new OpenCvSharp.Mat();
  1833. OpenCvSharp.Cv2.Compare(labels, i, mask, OpenCvSharp.CmpTypes.EQ); // 使用Cv2.Compare创建掩码
  1834. coloredRegion.SetTo(color, mask);
  1835. }
  1836. coloredRegion = ~coloredRegion;
  1837. //OpenCvSharp.Cv2.ImShow("coloredRegion", coloredRegion);
  1838. //OpenCvSharp.Cv2.WaitKey(0);
  1839. //OpenCvSharp.Cv2.DestroyAllWindows();
  1840. if (coloredParticleImage == null)
  1841. {
  1842. coloredParticleImage = new Bitmap(coloredRegion.ToBitmap());
  1843. }
  1844. else
  1845. {
  1846. coloredParticleImage = coloredRegion.ToBitmap();
  1847. }
  1848. labels.Dispose();
  1849. coloredRegion.Dispose();
  1850. }
  1851. private void aI自动识别ToolStripMenuItem_Click(object sender, EventArgs e)
  1852. {
  1853. aiProcess();
  1854. }
  1855. }
  1856. }