ToolWindow.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. using OTS.WinFormsUI.Docking;
  9. using OTSCommon;
  10. using OTSModelSharp.ServiceCenter;
  11. using OTSModelSharp.ServiceInterface;
  12. namespace OTSMeasureApp
  13. {
  14. public partial class ToolWindow : DockContent
  15. {
  16. //国际化
  17. OTSCommon.Language lan;
  18. Hashtable table;
  19. //测量主窗体
  20. public OTSIncAMeasureAppForm m_MeasureAppForm;
  21. public OTSMeasureStatusWindow MeasureStatuWindow;
  22. OTSImageData m_ImageData = null;
  23. NLog.Logger log ;
  24. public ToolWindow(OTSIncAMeasureAppForm m_MeasureForm, OTSMeasureStatusWindow MeasureStatuForm)
  25. {
  26. log = NLog.LogManager.GetCurrentClassLogger();
  27. InitializeComponent();
  28. m_MeasureAppForm = m_MeasureForm;
  29. MeasureStatuWindow = MeasureStatuForm;
  30. //国际化
  31. lan = new OTSCommon.Language(this);
  32. table = lan.GetNameTable(this.Name);
  33. }
  34. private Bitmap bseImg;
  35. /// <summary>
  36. /// 获取当前的BSE原图
  37. /// </summary>
  38. public Bitmap BseImg { get => bseImg; set => bseImg = value; }
  39. private byte[] bBseData;
  40. /// <summary>
  41. /// 获取当前的BSE原图数据
  42. /// </summary>
  43. public byte[] BBseData { get => bBseData; set => bBseData = value; }
  44. //去背景灰度最小值
  45. private int bseGrayMinValue=-1;
  46. /// <summary>
  47. /// 去背景灰度最小值
  48. /// </summary>
  49. public int BseGrayMinValue { get => bseGrayMinValue; set => bseGrayMinValue = value; }
  50. //去背景灰度最大值
  51. private int bseGrayMaxValue = -1;
  52. /// <summary>
  53. /// 去背景灰度最大值
  54. /// </summary>
  55. public int BseGrayMaxValue { get => bseGrayMaxValue; set => bseGrayMaxValue = value; }
  56. private void ToolWindow_Load(object sender, EventArgs e)
  57. {
  58. if (BseImg != null)
  59. {
  60. pbBSEImage.Image = BseImg;
  61. }
  62. if (BseGrayMinValue >-1)
  63. {
  64. nuDownGrayStart.Value = BseGrayMinValue;
  65. tbGrayStart.Value = BseGrayMinValue;
  66. txtGrayStart.Text = BseGrayMinValue.ToString();
  67. }
  68. if (BseGrayMaxValue >-1)
  69. {
  70. nuDownGrayEnd.Value = BseGrayMaxValue;
  71. tbGrayEnd.Value = BseGrayMaxValue;
  72. txtGrayEnd.Text = BseGrayMaxValue.ToString();
  73. }
  74. if (m_ImageData == null)
  75. {
  76. m_ImageData = new OTSImageData(MeasureStatuWindow, m_MeasureAppForm);
  77. }
  78. }
  79. #region BSE图去背景
  80. /// <summary>
  81. /// BSE图去背景
  82. /// </summary>
  83. /// <param name="bInput">BSE原图</param>
  84. /// <param name="grayStart">开始灰度值</param>
  85. /// <param name="grayEnd">结束灰度值</param>
  86. /// <returns></returns>
  87. protected Bitmap RemoveBseGray(Bitmap bInput,int grayStart, int grayEnd)
  88. {
  89. int imgWidth = bInput.Width;
  90. int imgHeight = bInput.Height;
  91. //转换颜色
  92. List<ColorMap> colorMapTemp = new List<ColorMap>();
  93. for (int i = grayStart; i <= grayEnd; i++)
  94. {
  95. ColorMap colorMap = new ColorMap();
  96. string colorName = "#" + Color.FromArgb(i, i, i).Name.ToString();
  97. colorMap.OldColor = ColorTranslator.FromHtml(colorName);
  98. colorMap.NewColor = Color.White;
  99. colorMapTemp.Add(colorMap);
  100. }
  101. if (colorMapTemp.Count > 0)
  102. {
  103. Bitmap cutbitmap = new Bitmap(bInput.Width, bInput.Height);
  104. //创建Graphics对象
  105. Graphics g = Graphics.FromImage(cutbitmap);
  106. //生成的图像大小
  107. int width = bInput.Width;
  108. int height = bInput.Height;
  109. //编辑被着急图像所要显示的位置
  110. Rectangle DrawRect = new Rectangle(0, 0, imgWidth, imgHeight);
  111. //编辑输出画布中着色的位置
  112. Rectangle ShowRect = new Rectangle(0, 0, imgWidth, imgHeight);
  113. ImageAttributes attr = new ImageAttributes();
  114. attr.SetRemapTable(colorMapTemp.ToArray());
  115. //从输入图像中截图至临时图像中
  116. g.DrawImage(bInput, ShowRect, 0, 0, DrawRect.Width, DrawRect.Height, GraphicsUnit.Pixel, attr);
  117. g.Dispose();
  118. MemoryStream ms = new MemoryStream();
  119. cutbitmap.Save(ms, ImageFormat.Png);
  120. cutbitmap.Dispose();
  121. //返回图像对象
  122. return (Bitmap)Image.FromStream(ms);
  123. }
  124. else
  125. {
  126. return null;
  127. }
  128. }
  129. #endregion
  130. #region 显示去背景BSE图
  131. public void ShowBSEImage(Bitmap BseImg)
  132. {
  133. if (BseImg != null)
  134. {
  135. pbBSEImage.Image = BseImg;
  136. pbBSEImage.Refresh();
  137. }
  138. }
  139. #endregion
  140. private void nuDwonGrayStart_ValueChanged(object sender, EventArgs e)
  141. {
  142. RemoveBseGrayValueChanged();
  143. }
  144. private void nuDownGrayEnd_ValueChanged(object sender, EventArgs e)
  145. {
  146. RemoveBseGrayValueChanged();
  147. }
  148. protected void RemoveBseGrayValueChanged()
  149. {
  150. int grayStart = (int)nuDownGrayStart.Value;
  151. int grayEnd = (int)nuDownGrayEnd.Value;
  152. if (grayStart <= grayEnd)
  153. {
  154. Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
  155. //Bitmap reBseImg = ShowRemoveBGImage(BBseData, grayStart, grayEnd);
  156. if (reBseImg != null)
  157. {
  158. ShowBSEImage(reBseImg);
  159. }
  160. }
  161. else
  162. {
  163. nuDownGrayEnd.Value = nuDownGrayStart.Value;
  164. }
  165. txtGrayStart.Text = nuDownGrayStart.Value.ToString();
  166. txtGrayEnd.Text = nuDownGrayEnd.Value.ToString();
  167. tbGrayStart.Value = (int)nuDownGrayStart.Value;
  168. tbGrayEnd.Value = (int)nuDownGrayEnd.Value;
  169. }
  170. private void tbGrayStart_Scroll(object sender, EventArgs e)
  171. {
  172. int grayStart = (int)tbGrayStart.Value;
  173. int grayEnd = (int)tbGrayEnd.Value;
  174. if (grayStart <= grayEnd)
  175. {
  176. Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
  177. //Bitmap reBseImg = ShowRemoveBGImage(BBseData, grayStart, grayEnd);
  178. if (reBseImg != null)
  179. {
  180. ShowBSEImage(reBseImg);
  181. }
  182. }
  183. else
  184. {
  185. tbGrayStart.Value = tbGrayStart.Value;
  186. tbGrayEnd.Value = tbGrayStart.Value;
  187. }
  188. txtGrayStart.Text = tbGrayStart.Value.ToString();
  189. txtGrayEnd.Text = tbGrayEnd.Value.ToString();
  190. nuDownGrayStart.Value = (int)tbGrayStart.Value;
  191. nuDownGrayEnd.Value = (int)tbGrayEnd.Value;
  192. }
  193. private void tbGrayEnd_Scroll(object sender, EventArgs e)
  194. {
  195. int grayStart = (int)tbGrayStart.Value;
  196. int grayEnd = (int)tbGrayEnd.Value;
  197. if (grayStart <= grayEnd)
  198. {
  199. txtGrayStart.Text = grayStart.ToString();
  200. txtGrayEnd.Text = grayEnd.ToString();
  201. Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
  202. //Bitmap reBseImg = ShowRemoveBGImage(BBseData, grayStart, grayEnd);
  203. if (reBseImg != null)
  204. {
  205. ShowBSEImage(reBseImg);
  206. }
  207. }
  208. else
  209. {
  210. tbGrayEnd.Value = tbGrayStart.Value;
  211. txtGrayEnd.Text = grayStart.ToString();
  212. }
  213. nuDownGrayStart.Value = tbGrayStart.Value;
  214. nuDownGrayEnd.Value = tbGrayEnd.Value;
  215. }
  216. private void btnYes_Click(object sender, EventArgs e)
  217. {
  218. BseGrayMinValue = Convert.ToInt32(txtGrayStart.Text);
  219. BseGrayMaxValue = Convert.ToInt32(txtGrayEnd.Text);
  220. this.DialogResult = DialogResult.Yes;
  221. }
  222. private void btnCancel_Click(object sender, EventArgs e)
  223. {
  224. this.DialogResult = DialogResult.Cancel;
  225. }
  226. #region BSE图去背景
  227. protected Bitmap ShowRemoveBGImage(byte[] bBseData,int grayStart,int grayEnd)
  228. {
  229. try
  230. {
  231. int m_iWidth = 0;
  232. int m_iHeight = 0;
  233. //获取电镜中图像大小
  234. string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolution();
  235. string[] sArray = str.Split('X');
  236. if (sArray[0] != "" && sArray[1] != "")
  237. {
  238. m_iWidth = Convert.ToInt32(sArray[0]);
  239. m_iHeight = Convert.ToInt32(sArray[1]);
  240. }
  241. //去背景图
  242. byte[] cBseData = null;
  243. //获取图像数据
  244. var imagefun = new OTSBSEImageFun();
  245. bool bfResult = imagefun.GetBSEImage(bBseData, m_iHeight, m_iWidth, grayStart, grayEnd, ref cBseData);
  246. if (bfResult)
  247. {
  248. Bitmap reImg= CImageHandler.ToGrayBitmap(cBseData, m_iWidth, m_iHeight);
  249. return reImg;
  250. }
  251. else
  252. {
  253. return null;
  254. }
  255. }
  256. catch (Exception ex)
  257. {
  258. log.Error("(ShowRemoveBGImage_Click):" + ex.ToString());
  259. }
  260. return null;
  261. }
  262. #endregion
  263. }
  264. }