123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Windows.Forms;
- using OTS.WinFormsUI.Docking;
- using OTSSysMgrTools;
- namespace OTSMeasureApp
- {
- public partial class ToolWindow : DockContent
- {
- //国际化
- OTSSysMgrTools.Language lan;
- Hashtable table;
- //测量主窗体
- public OTSIncAMeasureAppForm m_MeasureAppForm;
- public OTSMeasureStatusWindow MeasureStatuWindow;
- OTSImageData m_ImageData = null;
- NLog.Logger log ;
- public ToolWindow(OTSIncAMeasureAppForm m_MeasureForm, OTSMeasureStatusWindow MeasureStatuForm)
- {
- log = NLog.LogManager.GetCurrentClassLogger();
- InitializeComponent();
- m_MeasureAppForm = m_MeasureForm;
- MeasureStatuWindow = MeasureStatuForm;
- //国际化
- lan = new OTSSysMgrTools.Language(this);
- table = lan.GetNameTable(this.Name);
- }
- private Bitmap bseImg;
- /// <summary>
- /// 获取当前的BSE原图
- /// </summary>
- public Bitmap BseImg { get => bseImg; set => bseImg = value; }
- private byte[] bBseData;
- /// <summary>
- /// 获取当前的BSE原图数据
- /// </summary>
- public byte[] BBseData { get => bBseData; set => bBseData = value; }
- //去背景灰度最小值
- private int bseGrayMinValue=-1;
- /// <summary>
- /// 去背景灰度最小值
- /// </summary>
- public int BseGrayMinValue { get => bseGrayMinValue; set => bseGrayMinValue = value; }
- //去背景灰度最大值
- private int bseGrayMaxValue = -1;
- /// <summary>
- /// 去背景灰度最大值
- /// </summary>
- public int BseGrayMaxValue { get => bseGrayMaxValue; set => bseGrayMaxValue = value; }
- private void ToolWindow_Load(object sender, EventArgs e)
- {
- if (BseImg != null)
- {
- pbBSEImage.Image = BseImg;
- }
- if (BseGrayMinValue >-1)
- {
- nuDownGrayStart.Value = BseGrayMinValue;
- tbGrayStart.Value = BseGrayMinValue;
- txtGrayStart.Text = BseGrayMinValue.ToString();
- }
- if (BseGrayMaxValue >-1)
- {
- nuDownGrayEnd.Value = BseGrayMaxValue;
- tbGrayEnd.Value = BseGrayMaxValue;
- txtGrayEnd.Text = BseGrayMaxValue.ToString();
- }
- if (m_ImageData == null)
- {
- m_ImageData = new OTSImageData(MeasureStatuWindow, m_MeasureAppForm);
- }
- }
- #region BSE图去背景
- /// <summary>
- /// BSE图去背景
- /// </summary>
- /// <param name="bInput">BSE原图</param>
- /// <param name="grayStart">开始灰度值</param>
- /// <param name="grayEnd">结束灰度值</param>
- /// <returns></returns>
- protected Bitmap RemoveBseGray(Bitmap bInput,int grayStart, int grayEnd)
- {
- int imgWidth = bInput.Width;
- int imgHeight = bInput.Height;
- //转换颜色
- List<ColorMap> colorMapTemp = new List<ColorMap>();
- for (int i = grayStart; i <= grayEnd; i++)
- {
- ColorMap colorMap = new ColorMap();
- string colorName = "#" + Color.FromArgb(i, i, i).Name.ToString();
- colorMap.OldColor = ColorTranslator.FromHtml(colorName);
- colorMap.NewColor = Color.White;
- colorMapTemp.Add(colorMap);
- }
- if (colorMapTemp.Count > 0)
- {
- Bitmap cutbitmap = new Bitmap(bInput.Width, bInput.Height);
- //创建Graphics对象
- Graphics g = Graphics.FromImage(cutbitmap);
- //生成的图像大小
- int width = bInput.Width;
- int height = bInput.Height;
- //编辑被着急图像所要显示的位置
- Rectangle DrawRect = new Rectangle(0, 0, imgWidth, imgHeight);
- //编辑输出画布中着色的位置
- Rectangle ShowRect = new Rectangle(0, 0, imgWidth, imgHeight);
- ImageAttributes attr = new ImageAttributes();
- attr.SetRemapTable(colorMapTemp.ToArray());
- //从输入图像中截图至临时图像中
- g.DrawImage(bInput, ShowRect, 0, 0, DrawRect.Width, DrawRect.Height, GraphicsUnit.Pixel, attr);
- g.Dispose();
- MemoryStream ms = new MemoryStream();
- cutbitmap.Save(ms, ImageFormat.Png);
- cutbitmap.Dispose();
- //返回图像对象
- return (Bitmap)Image.FromStream(ms);
- }
- else
- {
- return null;
- }
- }
- #endregion
-
- #region 显示去背景BSE图
- public void ShowBSEImage(Bitmap BseImg)
- {
- if (BseImg != null)
- {
- pbBSEImage.Image = BseImg;
- pbBSEImage.Refresh();
- }
- }
- #endregion
- private void nuDwonGrayStart_ValueChanged(object sender, EventArgs e)
- {
- RemoveBseGrayValueChanged();
- }
- private void nuDownGrayEnd_ValueChanged(object sender, EventArgs e)
- {
- RemoveBseGrayValueChanged();
- }
- protected void RemoveBseGrayValueChanged()
- {
- int grayStart = (int)nuDownGrayStart.Value;
- int grayEnd = (int)nuDownGrayEnd.Value;
- if (grayStart <= grayEnd)
- {
- Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
- //Bitmap reBseImg = ShowRemoveBGImage(BBseData, grayStart, grayEnd);
- if (reBseImg != null)
- {
- ShowBSEImage(reBseImg);
- }
- }
- else
- {
- nuDownGrayEnd.Value = nuDownGrayStart.Value;
- }
- txtGrayStart.Text = nuDownGrayStart.Value.ToString();
- txtGrayEnd.Text = nuDownGrayEnd.Value.ToString();
- tbGrayStart.Value = (int)nuDownGrayStart.Value;
- tbGrayEnd.Value = (int)nuDownGrayEnd.Value;
- }
- private void tbGrayStart_Scroll(object sender, EventArgs e)
- {
- int grayStart = (int)tbGrayStart.Value;
- int grayEnd = (int)tbGrayEnd.Value;
- if (grayStart <= grayEnd)
- {
- Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
- //Bitmap reBseImg = ShowRemoveBGImage(BBseData, grayStart, grayEnd);
- if (reBseImg != null)
- {
- ShowBSEImage(reBseImg);
- }
- }
- else
- {
- tbGrayStart.Value = tbGrayStart.Value;
- tbGrayEnd.Value = tbGrayStart.Value;
- }
- txtGrayStart.Text = tbGrayStart.Value.ToString();
- txtGrayEnd.Text = tbGrayEnd.Value.ToString();
- nuDownGrayStart.Value = (int)tbGrayStart.Value;
- nuDownGrayEnd.Value = (int)tbGrayEnd.Value;
- }
- private void tbGrayEnd_Scroll(object sender, EventArgs e)
- {
- int grayStart = (int)tbGrayStart.Value;
- int grayEnd = (int)tbGrayEnd.Value;
- if (grayStart <= grayEnd)
- {
- txtGrayStart.Text = grayStart.ToString();
- txtGrayEnd.Text = grayEnd.ToString();
- Bitmap reBseImg = RemoveBseGray(BseImg, grayStart, grayEnd);
- //Bitmap reBseImg = ShowRemoveBGImage(BBseData, grayStart, grayEnd);
- if (reBseImg != null)
- {
- ShowBSEImage(reBseImg);
- }
- }
- else
- {
- tbGrayEnd.Value = tbGrayStart.Value;
- txtGrayEnd.Text = grayStart.ToString();
- }
- nuDownGrayStart.Value = tbGrayStart.Value;
- nuDownGrayEnd.Value = tbGrayEnd.Value;
- }
- private void btnYes_Click(object sender, EventArgs e)
- {
- BseGrayMinValue = Convert.ToInt32(txtGrayStart.Text);
- BseGrayMaxValue = Convert.ToInt32(txtGrayEnd.Text);
- this.DialogResult = DialogResult.Yes;
- }
- private void btnCancel_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.Cancel;
- }
- #region BSE图去背景
- protected Bitmap ShowRemoveBGImage(byte[] bBseData,int grayStart,int grayEnd)
- {
- try
- {
- int m_iWidth = 0;
- int m_iHeight = 0;
- //获取电镜中图像大小
- string str = m_MeasureAppForm.m_ProjParam.GetBSEImageResolution();
- string[] sArray = str.Split('X');
- if (sArray[0] != "" && sArray[1] != "")
- {
- m_iWidth = Convert.ToInt32(sArray[0]);
- m_iHeight = Convert.ToInt32(sArray[1]);
- }
- //去背景图
- byte[] cBseData = null;
- //获取图像数据
- var imagefun = new OTSBSEImageFun();
- bool bfResult = imagefun.GetBSEImage(bBseData, m_iHeight, m_iWidth, grayStart, grayEnd, ref cBseData);
- if (bfResult)
- {
- Bitmap reImg= Imagepro.ToGrayBitmap(cBseData, m_iWidth, m_iHeight);
- return reImg;
- }
- else
- {
- return null;
- }
- }
- catch (Exception ex)
- {
- log.Error("(ShowRemoveBGImage_Click):" + ex.ToString());
- }
- return null;
- }
- #endregion
- }
- }
|