版博士V2.0程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

785 rindas
34 KiB

  1. using Devart.Common;
  2. using ImageToolKits;
  3. using MaiMuAOI.SysCtrl;
  4. using MaiMuControl.Device;
  5. using Models;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using OpenCvSharp;
  9. using OpenCvSharp.Flann;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Drawing;
  15. using System.Drawing.Imaging;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows.Forms;
  21. using Yolo5;
  22. using static Mysqlx.Expect.Open.Types.Condition.Types;
  23. namespace MaiMuAOI.SysUI.DefectPicShow
  24. {
  25. public partial class DistributionFrm : Form
  26. {
  27. private Order _order;
  28. private Dictionary<string, List<List<string>>> defectInfoDir = new Dictionary<string, List<List<string>>>();
  29. private List<DefectStruct> defectList = new List<DefectStruct>();
  30. private Dictionary<string, Bitmap> defectBmpsDir = new Dictionary<string, Bitmap>();
  31. private Yolo_Class yolo = new Yolo_Class();
  32. private int currDefectIndex = 0;
  33. private Color[] colorList = { Color.Red, Color.Green, Color.DarkViolet , Color.Magenta, Color.Orange, Color.Brown,
  34. Color.Olive, Color.PaleGreen, Color.CadetBlue,Color.Aqua,Color.YellowGreen,Color.Blue,Color.SpringGreen,Color.Fuchsia,Color.White };
  35. private string[] DefectCodes = { "dk", "zw","xws","gsyc","qk", "zk","pp","hs","yx", "xb", "sx","ds"};
  36. #region 表格数据类型
  37. private class DefectStruct
  38. {
  39. //Defect_{order.SN}_{res.index}_X{res.Xmm}_Y{res.Ymm}
  40. public string Key { get; set; }
  41. public int CurrIndex { get; set; }//当前缺陷在本大图中的索引0-n
  42. public int MainIndex { get; set; }//大图
  43. public double MainX { get; set; }//大图X 大相机拍照时原始X,Y轴坐标
  44. public double MainY { get; set; }//大图X
  45. //["4","3.9","-0.8","dk","0.39"],["index","X","Y","缺陷类型","缺陷的置信度"]
  46. public int Index { get; set; }
  47. public double X { get; set; }
  48. public double Y { get; set; }
  49. public string DefectName { get; set; }//缺陷类型
  50. public double DefectCC { get; set; }//缺陷的置信度
  51. //2023-10-30 修复情况
  52. public string Restoratory { get; set; }
  53. public double Area { get; set; }
  54. }
  55. #endregion
  56. public DistributionFrm(Order order)
  57. {
  58. InitializeComponent();
  59. UIStyle.SetUIStyle(this);
  60. this.uiTitel1.FatherForm = this;
  61. _order = order;
  62. imageBox0.SetIconsVisible(false);
  63. imageBox0.ChangeCursorModeIsHand(true);
  64. //imageBox0.set
  65. imageBox1.SetIconsVisible(false);
  66. imageBox1.ChangeCursorModeIsHand(true);
  67. #region 图片列表设置
  68. this.dgvProcess.AutoGenerateColumns = false;
  69. //显示行号与列宽度自动调整
  70. dgvProcess.RowHeadersVisible = true;
  71. dgvProcess.RowHeadersWidth = 30;
  72. dgvProcess.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  73. //dgvProcess.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
  74. dgvProcess.RowPostPaint += (sender, e) =>//序号列头
  75. {
  76. showRowNum_onDataGrid_RowPostPaint(this.dgvProcess, sender, e);
  77. };
  78. for (int i = 0; i < dgvProcess.Columns.Count; i++)//禁止点击列头排序
  79. dgvProcess.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  80. dgvProcess.DefaultCellStyle.ForeColor = Color.Black;
  81. #endregion
  82. #region 图纸列表设置
  83. this.dataGridView1.AutoGenerateColumns = false;
  84. //显示行号与列宽度自动调整
  85. dataGridView1.RowHeadersVisible = true;
  86. dataGridView1.RowHeadersWidth = 30;
  87. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  88. //dgvProcess.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
  89. dataGridView1.RowPostPaint += (sender, e) =>//序号列头
  90. {
  91. showRowNum_onDataGrid_RowPostPaint(this.dataGridView1, sender, e);
  92. };
  93. for (int i = 0; i < dataGridView1.Columns.Count; i++)//禁止点击列头排序
  94. dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  95. dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
  96. #endregion
  97. //图片查看功能现阶段禁用,存在bug
  98. tabControl1.TabPages.Remove(this.tabPage1);
  99. }
  100. //数据表重绘
  101. private void showRowNum_onDataGrid_RowPostPaint(DataGridView dgv, object sender, DataGridViewRowPostPaintEventArgs e)
  102. {
  103. Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
  104. TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  105. }
  106. #region 界面调整
  107. private void DistributionFrm_SizeChanged(object sender, EventArgs e)
  108. {
  109. if (this.WindowState == FormWindowState.Maximized)
  110. {
  111. this.WindowState = FormWindowState.Normal;
  112. this.Top = 0;
  113. this.Left = 0;
  114. this.Width = SystemInformation.WorkingArea.Width;
  115. this.Height = SystemInformation.WorkingArea.Height;
  116. }
  117. }
  118. #endregion
  119. #region 获取缺陷代码对应缺陷项
  120. public string getDefectName(string defectCode)
  121. {
  122. defectCode = defectCode.ToLower();
  123. switch (defectCode)
  124. {
  125. case "dk":
  126. return "堵孔";
  127. case "zw":
  128. return "脏污";
  129. case "xws":
  130. return "纤维丝";
  131. case "gsyc":
  132. return "钢丝异常";
  133. case "qk":
  134. return "缺口";
  135. case "zk":
  136. return "针孔";
  137. case "pp":
  138. return "泡泡";
  139. case "hs":
  140. return "划伤";
  141. case "yx":
  142. return "压线";
  143. case "xb":
  144. return "斜边";
  145. case "sx":
  146. return "栅线";
  147. case "ds":
  148. return "断栅";
  149. default:
  150. return "未知";
  151. }
  152. }
  153. #endregion
  154. #region 数据获取
  155. private bool GetDefectDataFromSN(string sn, string date)
  156. {
  157. try
  158. {
  159. JObject data = new JObject();
  160. string path, key;
  161. string[] files;
  162. #region 获取所有缺陷数据
  163. //文件名格式:$"Size_SN{order.SN}_I{res.index}_X{res.Defects_X}_Y{res.Defects_Y}_.....json/bmp
  164. //--缺陷
  165. path = $"{ConfMgr.Instance.SysConfigParams.DefectRepairImag.SavePath}\\{date}\\{sn}\\";
  166. if (Directory.Exists(path))
  167. {
  168. files = Directory.GetFiles(path, $"Defect_SN{sn}_*.json", SearchOption.TopDirectoryOnly);
  169. //对文件名中的数字(index)进行大小排序
  170. files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
  171. foreach (string file in files)
  172. {
  173. key = Path.GetFileNameWithoutExtension(file);
  174. if (File.Exists(path + key + ".bmp"))
  175. data.Add(key, JArray.Parse(File.ReadAllText(file)));
  176. //加载小图
  177. //LoadDefectPic(new Bitmap(path + key + ".bmp"), key);
  178. }
  179. }
  180. //--缺陷加载小图
  181. path = $"{ConfMgr.Instance.SysConfigParams.DefectSmallImag.SavePath}\\{date}\\{sn}\\";
  182. if (Directory.Exists(path))
  183. {
  184. files = Directory.GetFiles(path, $"Defect_SN{sn}_*.bmp", SearchOption.TopDirectoryOnly);
  185. //对文件名中的数字(index)进行大小排序
  186. files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
  187. foreach (string file in files)
  188. {
  189. key = Path.GetFileNameWithoutExtension(file);
  190. //加载小图
  191. LoadDefectPic(new Bitmap(file), key);
  192. }
  193. }
  194. //--比对
  195. path = $"{ConfMgr.Instance.SysConfigParams.SizeRepairImag.SavePath}\\{date}\\{sn}\\";
  196. if (Directory.Exists(path))
  197. {
  198. files = Directory.GetFiles(path, $"Size_SN{sn}_*.json", SearchOption.TopDirectoryOnly);
  199. //对文件名中的数字(index)进行大小排序
  200. files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
  201. foreach (string file in files)
  202. {
  203. key = Path.GetFileNameWithoutExtension(file);
  204. if (File.Exists(path + key + ".bmp"))
  205. data.Add(key, JArray.Parse(File.ReadAllText(file)));
  206. }
  207. }
  208. #endregion
  209. #region 加载图片和数据
  210. string file_path;
  211. foreach (var kv in data)
  212. {
  213. defectInfoDir.Add(kv.Key, JsonConvert.DeserializeObject<List<List<string>>>(kv.Value.ToString()));
  214. file_path = $"{ConfMgr.Instance.SysConfigParams.DefectRepairImag.SavePath}\\{date}\\{sn}\\{kv.Key}.bmp";
  215. if (!File.Exists(file_path))
  216. throw new Exception(kv.Key + " 缺陷文件不存在!");
  217. Bitmap bmp = new Bitmap(file_path);
  218. defectBmpsDir.Add(kv.Key, bmp);
  219. }
  220. defectList.Clear();
  221. if (File.Exists(ConfMgr.Instance.SysConfigParams.ImageProcessDataPath))
  222. {
  223. double X, Y;
  224. int MainIndex;
  225. foreach (var item in defectInfoDir)
  226. {
  227. //Key : Defect_SN{order.SN}_I{res.index}_X{res.Xmm}_Y{res.Ymm}_C{res.defectCount}
  228. string[] str = item.Key.Split('_');
  229. MainIndex = Convert.ToInt32(str[2].Substring(1));
  230. X = Convert.ToDouble(str[3].Substring(1));
  231. Y = Convert.ToDouble(str[4].Substring(1));
  232. var arr = yolo.ImageXY2RepairTable(item.Value, X, Y, ConfMgr.Instance.SysConfigParams.ImageProcessDataPath);
  233. int i = 0;
  234. foreach (var arr2 in arr)
  235. {
  236. var obj = new DefectStruct();
  237. obj.Key = item.Key;
  238. obj.CurrIndex = i++;
  239. obj.MainIndex = MainIndex;
  240. obj.MainX = X;
  241. obj.MainY = Y;
  242. obj.Index = Convert.ToInt32(arr2[0].ToString());
  243. obj.X = Convert.ToDouble(arr2[1].ToString());
  244. obj.Y = Convert.ToDouble(arr2[2].ToString());
  245. obj.DefectName = getDefectName(arr2[3].ToString());
  246. obj.DefectCC = Convert.ToDouble(arr2[4].ToString());
  247. defectList.Add(obj);
  248. }
  249. }
  250. this.Invoke(new System.Action(() =>
  251. {
  252. this.dgvProcess.DataSource = null;
  253. //注:这样绑定List中的对象必需得用{get;set;}方式定义属性,否则单独格为空内容
  254. this.dgvProcess.DataSource = new BindingSource(defectList, null);
  255. this.gpbDefectList.Text = $"缺陷明细({defectList.Count}条)";
  256. }));
  257. gotoDefctListIndex(currDefectIndex);
  258. }
  259. else
  260. throw new Exception("转换文件不存在!Path:" + ConfMgr.Instance.SysConfigParams.ImageProcessDataPath);
  261. /*
  262. string file_path;
  263. bool first = true;
  264. foreach (var kv in data)
  265. {
  266. defectInfoDir.Add(kv.Key, JsonConvert.DeserializeObject<List<List<string>>>(kv.Value.ToString()));
  267. file_path = $"{ConfMgr.Instance.SysConfigParams.DefectRepairImag.SavePath}\\{date}\\{sn}\\{kv.Key}.bmp";
  268. if (!File.Exists(file_path))
  269. throw new Exception(kv.Key + " 缺陷文件不存在!");
  270. Bitmap bmp = new Bitmap(file_path);
  271. defectBmpsDir.Add(kv.Key, bmp);
  272. //["4","3.9","-0.8","dk","0.39"],["index","X","Y","缺陷类型","缺陷的置信度"]
  273. JArray arr = kv.Value as JArray;//二维数组
  274. foreach (JArray arr2 in arr)
  275. {
  276. var obj = new DefectStruct();
  277. obj.Key = kv.Key;
  278. obj.MainIndex = int.Parse(kv.Key.Split('_')[2].Substring(1));
  279. obj.Index = Convert.ToInt32(arr2[0].ToString());
  280. obj.X = Convert.ToDouble(arr2[1].ToString());
  281. obj.Y = Convert.ToDouble(arr2[2].ToString());
  282. obj.DefectName = getDefectName(arr2[3].ToString());
  283. obj.DefectCC = Convert.ToDouble(arr2[4].ToString());
  284. defectList.Add(obj);
  285. }
  286. if (first)
  287. {
  288. first = false;
  289. this.Invoke(new System.Action(() =>
  290. {
  291. this.reloadPic(bmp, defectInfoDir[defectList[0].Key], defectList[0]);
  292. //Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
  293. //mat = yolo.RepairTableDrawRec(defectInfor2RepairTable, defectInfo.MainX, defectInfo.MainY, defectInfo.CurrIndex, mat);
  294. }));
  295. }
  296. }
  297. this.Invoke(new System.Action(() =>
  298. {
  299. //注:这样绑定List中的对象必需得用{get;set;}方式定义属性,否则单独格为空内容
  300. this.dgvProcess.DataSource = new BindingSource(defectList, null);
  301. this.gpbDefectList.Text = $"缺陷明细({defectList.Count}条)";
  302. //2023-11-1 图纸对比缺陷标红
  303. for (int i = 0; i < defectList.Count; i++)
  304. {
  305. string defName = (string)this.dgvProcess.Rows[i].Cells["colDefectName"].Value;
  306. if ((defName == getDefectName("ds")) || ((defName == getDefectName("yx"))))
  307. this.dgvProcess.Rows[i].DefaultCellStyle.BackColor = Color.Red;
  308. }
  309. }));*/
  310. #endregion
  311. }
  312. catch (Exception ex)
  313. {
  314. MessageBox.Show(ex.Message, "加载错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  315. return false;
  316. }
  317. return true;
  318. }
  319. #endregion
  320. #region 图片加载
  321. private void reloadPic(Bitmap bmp, List<List<string>> defectInfor2RepairTable, DefectStruct defectInfo)
  322. {
  323. //绘框
  324. if (defectInfor2RepairTable != null && defectInfo != null)
  325. {
  326. Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
  327. mat = yolo.RepairTableDrawRec(defectInfor2RepairTable, defectInfo.MainX, defectInfo.MainY, defectInfo.CurrIndex, mat);
  328. //mat.ImWrite("ttt.bmp");
  329. imageBox0.RefreshWindow(mat);
  330. }
  331. }
  332. #endregion
  333. #region 表格事件
  334. private void dgvProcess_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  335. {
  336. int preIndex = currDefectIndex;
  337. currDefectIndex = e.RowIndex;
  338. gotoDefctListIndex(currDefectIndex, preIndex);
  339. //图片对应不上,不使用
  340. //Control[] cts = this.pnlBmpList.Controls.Find("imgDefect_" + (currDefectIndex), true);
  341. //if((cts != null)&&(cts.Length>0))
  342. //{
  343. // cts[0].Select();
  344. //}
  345. }
  346. private void data_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  347. {
  348. int preIndex = currDefectIndex;
  349. currDefectIndex = e.RowIndex;
  350. gotoDefctListIndex2(currDefectIndex, preIndex);
  351. //图片对应不上,不使用
  352. //Control[] cts = this.pnlBmpList.Controls.Find("imgDefect_" + (currDefectIndex), true);
  353. //if((cts != null)&&(cts.Length>0))
  354. //{
  355. // cts[0].Select();
  356. //}
  357. Control[] cts = this.flowLayoutPanel1.Controls.Find("imgDefect_" + (defectList[currDefectIndex].Index), true);
  358. if ((cts != null) && (cts.Length > 0))
  359. {
  360. cts[0].Select();
  361. }
  362. }
  363. #endregion
  364. #region 选择缺陷
  365. private void gotoDefctListIndex(int index, int preIndex = -1)
  366. {
  367. try
  368. {
  369. this.Invoke(new System.Action(() =>
  370. {
  371. string key = defectList[index].Key;
  372. if (defectBmpsDir.ContainsKey(key))
  373. {
  374. reloadPic(defectBmpsDir[key], defectInfoDir[key], defectList[index]);
  375. }
  376. if (preIndex > -1)
  377. this.dgvProcess.Rows[preIndex].DefaultCellStyle.BackColor = Color.White;
  378. this.dgvProcess.Rows[index].DefaultCellStyle.BackColor = Color.LightGreen;
  379. this.dgvProcess.Rows[index].Selected = true;
  380. //this.dgvProcess.CurrentCell = this.dgvProcess.Rows[index].Cells["colResult"];
  381. //this.dgvProcess.Rows[index].Cells["colResult"].Value = "已检";
  382. //reDrawByIndex(index);
  383. }));
  384. if ((defectList[index].X <= 0) || (defectList[index].Y <= 0))
  385. {
  386. return;
  387. }
  388. }
  389. catch (Exception ex)
  390. {
  391. }
  392. }
  393. private void gotoDefctListIndex2(int index, int preIndex = -1)
  394. {
  395. try
  396. {
  397. this.Invoke(new System.Action(() =>
  398. {
  399. if (preIndex > -1)
  400. this.dataGridView1.Rows[preIndex].DefaultCellStyle.BackColor = Color.White;
  401. this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.LightGreen;
  402. this.dataGridView1.Rows[index].Selected = true;
  403. reDrawByIndex(index);
  404. }));
  405. }
  406. catch (Exception ex)
  407. {
  408. }
  409. }
  410. #endregion
  411. #region 图纸显示
  412. private string _imgPath;
  413. private void reDraw(string imgPath)
  414. {
  415. _imgPath = imgPath;
  416. Bitmap bmp;
  417. Graphics g;
  418. using (System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath))
  419. {
  420. if (IsIndexedPixelFormat(img.PixelFormat))
  421. {
  422. bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
  423. g = Graphics.FromImage(bmp);
  424. g.DrawImage(img, 0, 0);
  425. }
  426. else
  427. {
  428. bmp = new Bitmap(imgPath);
  429. g = Graphics.FromImage(bmp);
  430. }
  431. }
  432. var list = _order.DefectInfoList;
  433. int x, y, w, h;
  434. int i = 0;
  435. int index = 0;
  436. foreach (string code in DefectCodes)
  437. {
  438. var lstDefectCode = list.Where(m => m.Code == code ).ToList();
  439. foreach (var info in lstDefectCode)
  440. {
  441. x = (int)info.X - 10;
  442. y = (int)info.Y - 10;
  443. w = h = 20;
  444. if (x < 0) x = 0;
  445. if (y < 0) y = 0;
  446. if (w > bmp.Width - 1) w = bmp.Width - 1;
  447. if (h > bmp.Height - 1) h = bmp.Height - 1;
  448. //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
  449. g.FillEllipse(new SolidBrush(colorList[i]), x, y, w, h);
  450. var obj = new DefectStruct();
  451. //obj.Key = info.Pid;
  452. obj.CurrIndex = index++;
  453. //obj.MainIndex = MainIndex;
  454. //obj.MainX = X;
  455. //obj.MainY = Y;
  456. //obj.Index = Convert.ToInt32(arr2[0].ToString());
  457. obj.X = info.X;
  458. obj.Y = info.Y;
  459. obj.Index = info.Index;
  460. obj.DefectName = getDefectName(info.Code);
  461. obj.DefectCC = info.ZXD;
  462. obj.Area = info.Area;
  463. defectList.Add(obj);
  464. }
  465. i++;
  466. }
  467. g.Flush();
  468. g.Dispose();
  469. this.Invoke(new System.Action(() =>
  470. {
  471. this.dataGridView1.DataSource = null;
  472. //注:这样绑定List中的对象必需得用{get;set;}方式定义属性,否则单独格为空内容
  473. this.dataGridView1.DataSource = new BindingSource(defectList, null);
  474. this.groupBox1.Text = $"缺陷明细({defectList.Count}条)";
  475. }));
  476. //
  477. //reloadPic(bmp);
  478. //ucImageView1.loadImage(bmp);
  479. OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);//用bitmap转换为mat
  480. imageBox1.RefreshWindow(mat);
  481. }
  482. private void reDrawByIndex(int index)
  483. {
  484. Bitmap bmp;
  485. Graphics g;
  486. using (System.Drawing.Image img = System.Drawing.Image.FromFile(_imgPath))
  487. {
  488. if (IsIndexedPixelFormat(img.PixelFormat))
  489. {
  490. bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
  491. g = Graphics.FromImage(bmp);
  492. g.DrawImage(img, 0, 0);
  493. }
  494. else
  495. {
  496. bmp = new Bitmap(_imgPath);
  497. g = Graphics.FromImage(bmp);
  498. }
  499. }
  500. var list = _order.DefectInfoList;
  501. int x, y, w, h;
  502. int i = 0;
  503. int t_index = 0;
  504. foreach (string code in DefectCodes)
  505. {
  506. var lstDefectCode = list.Where(m => m.Code == code).ToList();
  507. foreach (var info in lstDefectCode)
  508. {
  509. if (t_index == index)
  510. {
  511. x = (int)info.X - 20;
  512. y = (int)info.Y - 20;
  513. w = h = 40;
  514. if (x < 0) x = 0;
  515. if (y < 0) y = 0;
  516. if (w > bmp.Width - 1) w = bmp.Width - 1;
  517. if (h > bmp.Height - 1) h = bmp.Height - 1;
  518. //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
  519. g.FillRectangle(new SolidBrush(Color.LimeGreen), x, y, w, h);
  520. }
  521. else
  522. {
  523. x = (int)info.X - 10;
  524. y = (int)info.Y - 10;
  525. w = h = 20;
  526. if (x < 0) x = 0;
  527. if (y < 0) y = 0;
  528. if (w > bmp.Width - 1) w = bmp.Width - 1;
  529. if (h > bmp.Height - 1) h = bmp.Height - 1;
  530. //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
  531. g.FillEllipse(new SolidBrush(colorList[i]), x, y, w, h);
  532. }
  533. t_index++;
  534. }
  535. i++;
  536. }
  537. g.Flush();
  538. g.Dispose();
  539. //
  540. //reloadPic(bmp);
  541. //ucImageView1.loadImage(bmp);
  542. OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);//用bitmap转换为mat
  543. imageBox1.RefreshWindow(mat, ImageBox.ImageModeEnum.Part);
  544. }
  545. /// <summary>
  546. /// 判断图片是否索引像素格式,是否是引发异常的像素格式
  547. /// </summary>
  548. /// <param name="imagePixelFormat">图片的像素格式</param>
  549. /// <returns></returns>
  550. private bool IsIndexedPixelFormat(System.Drawing.Imaging.PixelFormat imagePixelFormat)
  551. {
  552. PixelFormat[] pixelFormatArray = {
  553. PixelFormat.Format1bppIndexed
  554. ,PixelFormat.Format4bppIndexed
  555. ,PixelFormat.Format8bppIndexed
  556. ,PixelFormat.Undefined
  557. ,PixelFormat.DontCare
  558. ,PixelFormat.Format16bppArgb1555
  559. ,PixelFormat.Format16bppGrayScale
  560. };
  561. foreach (PixelFormat pf in pixelFormatArray)
  562. {
  563. if (imagePixelFormat == pf)
  564. {
  565. return true;
  566. }
  567. }
  568. return false;
  569. }
  570. #endregion
  571. #region 界面加载
  572. private void DistributionFrm_Load(object sender, EventArgs e)
  573. {
  574. this.gboxDefectList.Tag = 0;
  575. this.pnlBmpList.Controls.Clear();
  576. this.flowLayoutPanel1.Controls.Clear();
  577. this.Cursor = Cursors.WaitCursor;
  578. #region 加载图纸缺陷查看
  579. Models.Attachment attachmentFile = _order.ProductInfo.AttachmentList.FirstOrDefault(x => x.Type == 0);
  580. string gbxBmpPath = "";
  581. if (attachmentFile != null)
  582. {
  583. gbxBmpPath = ConfMgr.Instance.ProjectDir + $"\\{attachmentFile.NameTimestamp}";
  584. if (!File.Exists(gbxBmpPath + ".bmp"))
  585. gbxBmpPath = "";
  586. }
  587. if (string.IsNullOrEmpty(gbxBmpPath))
  588. {
  589. MessageBox.Show("图纸文件不存在!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  590. //this.Close();
  591. }
  592. else
  593. {
  594. if (!File.Exists(gbxBmpPath + ".jpg"))//转背景后的图纸文件
  595. {
  596. //换背景JPG
  597. Mat mat = Cv2.ImRead(gbxBmpPath + ".bmp");
  598. Cv2.CvtColor(mat, mat, ColorConversionCodes.RGB2GRAY);//转灰度图
  599. for (int i = 0; i < mat.Height; i++)
  600. {
  601. for (int j = 0; j < mat.Width; j++)
  602. {
  603. if (mat.At<byte>(i, j) == 255)//白色
  604. mat.Set<byte>(i, j, 0);
  605. else
  606. mat.Set<byte>(i, j, 255);
  607. }
  608. }
  609. OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat).Save(gbxBmpPath + ".jpg", ImageFormat.Jpeg);
  610. }
  611. string imagePath = gbxBmpPath + ".jpg";
  612. reDraw(imagePath);
  613. //--缺陷加载小图
  614. string[] files;
  615. string key;
  616. string path = $"{ConfMgr.Instance.SysConfigParams.DefectSmallImag.SavePath}\\{_order.CreateTime.ToString("yyyMMdd")}\\{_order.SN}\\";
  617. if (Directory.Exists(path))
  618. {
  619. files = Directory.GetFiles(path, $"Defect_SN{_order.SN}_*.bmp", SearchOption.TopDirectoryOnly);
  620. //files = Directory.GetFiles(path, $"*.bmp", SearchOption.TopDirectoryOnly);
  621. //对文件名中的数字(index)进行大小排序
  622. //files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s))).ToArray();
  623. files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
  624. foreach (string file in files)
  625. {
  626. key = Path.GetFileNameWithoutExtension(file).Split('_')[7];
  627. //MessageBox.Show($"{file}, {key}");
  628. //加载小图
  629. LoadDefectPic(new Bitmap(file), key);
  630. }
  631. }
  632. }
  633. #endregion
  634. #if false
  635. #region 加载大图缺陷查看
  636. if (!GetDefectDataFromSN(_order.SN, _order.CreateTime.ToString("yyyMMdd")))
  637. {
  638. //this.Close();
  639. }
  640. #endregion
  641. #endif
  642. pnlBmpList.VerticalScroll.Value = 0;
  643. flowLayoutPanel1.VerticalScroll.Value = 0;
  644. this.Cursor = Cursors.Default;
  645. }
  646. #endregion
  647. #region 加载小图
  648. private void LoadDefectPic(Bitmap bitmap, string name)
  649. {
  650. {
  651. //MessageBox.Show($"0");
  652. //加载缺陷小图
  653. //纵向显示
  654. int imgWidth = this.flowLayoutPanel1.ClientSize.Width - 50;
  655. //MessageBox.Show($"01-{this.flowLayoutPanel1.ClientSize.Width}");
  656. int imgHeight = (int)((bitmap.Height * 1.0f / bitmap.Width) * imgWidth + 0.5);
  657. //MessageBox.Show($"02 - {bitmap.Height} - {bitmap.Width}");
  658. int splitWidth = 20;
  659. //MessageBox.Show($"03");
  660. int pnlWidth = this.flowLayoutPanel1.ClientSize.Width;
  661. //MessageBox.Show($"04 - {this.flowLayoutPanel1.ClientSize.Width}");
  662. //int index = (int)this.groupBox2.Tag;
  663. //MessageBox.Show($"1 - {this.groupBox2.Tag}");
  664. PictureBox picbox = new PictureBox();
  665. picbox.Width = imgWidth;
  666. picbox.Height = imgHeight;
  667. picbox.Image = (Bitmap)bitmap.Clone();
  668. picbox.Name = "imgDefect_" + name;
  669. picbox.Tag = name;
  670. //MessageBox.Show($"2");
  671. picbox.Click += new EventHandler(defectBmpBox_Click);
  672. picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  673. picbox.BorderStyle = BorderStyle.FixedSingle;
  674. //picbox.Margin = new Padding((pnlWidth - picbox.Height) / 2, splitWidth, 0, 0);
  675. picbox.Margin = new Padding((pnlWidth - picbox.Width) / 2, splitWidth, 0, 0);
  676. picbox.MouseHover += simpleTip_MouseHover;
  677. picbox.Visible = true;
  678. //MessageBox.Show($"3");
  679. this.flowLayoutPanel1.Controls.Add(picbox);
  680. flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.Maximum;
  681. //this.groupBox2.Tag = ++index;
  682. //this.groupBox2.Text = $"缺陷图像:{index} 张";
  683. //MessageBox.Show($"4");
  684. }
  685. }
  686. private void defectBmpBox_Click(object sender, EventArgs e)
  687. {
  688. //对应不上,不使用
  689. //PictureBox pb = sender as PictureBox;
  690. //int index = int.Parse(pb.Name.Replace("imgDefect_", ""));
  691. //dgvProcess_CellDoubleClick(null, new DataGridViewCellEventArgs(1, index));
  692. PictureBox pb = sender as PictureBox;
  693. int index = int.Parse(pb.Name.Replace("imgDefect_", ""));
  694. //MessageBox.Show($"{index}");
  695. for (int i = 0;i < defectList.Count; i++)
  696. {
  697. if (defectList[i].Index == index)
  698. {
  699. //MessageBox.Show($"111{index}");
  700. data_CellDoubleClick(null, new DataGridViewCellEventArgs(1, i));
  701. return;
  702. }
  703. }
  704. }
  705. private void simpleTip_MouseHover(object sender, EventArgs e)
  706. {
  707. PictureBox pb = sender as PictureBox;
  708. // 创建the ToolTip
  709. ToolTip toolTip1 = new ToolTip();
  710. // 设置显示样式
  711. toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
  712. toolTip1.InitialDelay = 500;//事件触发多久后出现提示
  713. toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
  714. toolTip1.ShowAlways = true;//是否显示提示框
  715. // 设置伴随的对象.
  716. toolTip1.SetToolTip(pb, pb.Name + "[" + pb.Tag.ToString() + "]");//设置提示按钮和提示内容
  717. }
  718. #endregion
  719. }
  720. }