ToolWindow.cs 9.8 KB

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