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