ToolWindow.cs 9.8 KB

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