版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

182 lines
6.5 KiB

  1. using Devart.Common;
  2. using MaiMuAOI.SysCtrl;
  3. using Models;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Drawing.Imaging;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace MaiMuAOI.SysUI.DefectPicShow
  16. {
  17. public partial class DefectImageShowFrm : Form
  18. {
  19. private Color[] colorList = { Color.Red, Color.Green, Color.DarkViolet , Color.Magenta, Color.Orange, Color.Brown,
  20. Color.Olive, Color.PaleGreen, Color.CadetBlue,Color.Aqua,Color.YellowGreen,Color.Blue,
  21. Color.SpringGreen,Color.Fuchsia,Color.Wheat,Color.AliceBlue,Color.Azure };
  22. private Order mOrder;
  23. private string imgPath;
  24. private int defectType;
  25. /// <summary>
  26. /// 0-Defect 1-Size
  27. /// </summary>
  28. /// <param name="_imgPath"></param>
  29. /// <param name="order"></param>
  30. /// <param name="_defectType">0-Defect 1-Size</param>
  31. public DefectImageShowFrm(string _imgPath, Order order, int _defectType = 0)
  32. {
  33. InitializeComponent();
  34. UIStyle.SetUIStyle(this);
  35. this.uiTitel1.FatherForm = this;
  36. mOrder = order;
  37. imgPath = _imgPath;
  38. defectType = _defectType;
  39. if (defectType == 0)
  40. {
  41. this.Text = "缺陷分布图";
  42. }
  43. else
  44. {
  45. this.Text = "比对失败区域分布";
  46. }
  47. }
  48. private void initDefectMenus()
  49. {
  50. if (mOrder == null || mOrder.DefectInfoList == null)
  51. throw new Exception("记录项为空!");
  52. tsbtnDefectList.DropDownItems.Clear();
  53. //全缺陷项
  54. var lstDefect = ConfMgr.GetArrayList<DefectCodeEnum>();
  55. foreach (DictionaryEntry item in lstDefect)
  56. {
  57. string code = item.Value.ToString();//zx item.Key=0/1/2/3
  58. int key = int.Parse(item.Key.ToString());
  59. string name = ((DefectNameEnum)key).ToString();
  60. int num = mOrder.DefectInfoList.Where(x => x.Code == code && x.Type == defectType).Count();//缺陷项个数
  61. ToolStripMenuItem menuItem = new ToolStripMenuItem() { Name = $"btnSub_{code}", Tag = code, Text = $"{name}({num})", ForeColor = colorList[key], Checked = true };
  62. tsbtnDefectList.DropDownItems.Add(menuItem);
  63. menuItem.Click += (object sender, EventArgs e) =>
  64. {
  65. menuItem.Checked = !menuItem.Checked;
  66. //if(menuItem.num>0)
  67. reDraw();
  68. };
  69. }
  70. }
  71. private void FrmShowSizeTag_Load(object sender, EventArgs e)
  72. {
  73. try
  74. {
  75. initDefectMenus();
  76. reDraw();
  77. }
  78. catch (Exception ex)
  79. {
  80. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  81. }
  82. }
  83. private void tsbtnRefesh_Click(object sender, EventArgs e)
  84. {
  85. try
  86. {
  87. initDefectMenus();
  88. reDraw();
  89. }
  90. catch (Exception ex)
  91. {
  92. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  93. }
  94. }
  95. private void tsbtnClose_Click(object sender, EventArgs e)
  96. {
  97. this.Close();
  98. }
  99. private void reDraw()
  100. {
  101. Bitmap bmp;
  102. Graphics g;
  103. using (System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath))
  104. {
  105. if (IsIndexedPixelFormat(img.PixelFormat))
  106. {
  107. bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
  108. g = Graphics.FromImage(bmp);
  109. g.DrawImage(img, 0, 0);
  110. }
  111. else
  112. {
  113. bmp = new Bitmap(imgPath);
  114. g = Graphics.FromImage(bmp);
  115. }
  116. }
  117. var list = mOrder.DefectInfoList;
  118. string code;
  119. int x, y, w, h;
  120. foreach (ToolStripMenuItem item in tsbtnDefectList.DropDownItems)
  121. {
  122. if (!item.Checked) continue;
  123. code = item.Tag.ToString();
  124. var lstDefectCode = list.Where(m => m.Code == code && m.Type == defectType).ToList();
  125. foreach (var info in lstDefectCode)
  126. {
  127. x = (int)info.X - 10;
  128. y = (int)info.Y - 10;
  129. w = h = 20;
  130. if (x < 0) x = 0;
  131. if (y < 0) y = 0;
  132. if (w > bmp.Width - 1) w = bmp.Width - 1;
  133. if (h > bmp.Height - 1) h = bmp.Height - 1;
  134. //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
  135. g.FillEllipse(new SolidBrush(item.ForeColor), x, y, w, h);
  136. }
  137. }
  138. g.Flush();
  139. g.Dispose();
  140. //
  141. //reloadPic(bmp);
  142. //ucImageView1.loadImage(bmp);
  143. OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);//用bitmap转换为mat
  144. imageBox1.RefreshWindow(mat);
  145. }
  146. /// <summary>
  147. /// 判断图片是否索引像素格式,是否是引发异常的像素格式
  148. /// </summary>
  149. /// <param name="imagePixelFormat">图片的像素格式</param>
  150. /// <returns></returns>
  151. private bool IsIndexedPixelFormat(System.Drawing.Imaging.PixelFormat imagePixelFormat)
  152. {
  153. PixelFormat[] pixelFormatArray = {
  154. PixelFormat.Format1bppIndexed
  155. ,PixelFormat.Format4bppIndexed
  156. ,PixelFormat.Format8bppIndexed
  157. ,PixelFormat.Undefined
  158. ,PixelFormat.DontCare
  159. ,PixelFormat.Format16bppArgb1555
  160. ,PixelFormat.Format16bppGrayScale
  161. };
  162. foreach (PixelFormat pf in pixelFormatArray)
  163. {
  164. if (imagePixelFormat == pf)
  165. {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. }
  172. }