版博士V2.0程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

176 рядки
6.1 KiB

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