版博士V2.0程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

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