|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785 |
- using Devart.Common;
- using ImageToolKits;
- using MaiMuAOI.SysCtrl;
- using MaiMuControl.Device;
- using Models;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using OpenCvSharp;
- using OpenCvSharp.Flann;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Yolo5;
- using static Mysqlx.Expect.Open.Types.Condition.Types;
-
- namespace MaiMuAOI.SysUI.DefectPicShow
- {
- public partial class DistributionFrm : Form
- {
- private Order _order;
- private Dictionary<string, List<List<string>>> defectInfoDir = new Dictionary<string, List<List<string>>>();
- private List<DefectStruct> defectList = new List<DefectStruct>();
- private Dictionary<string, Bitmap> defectBmpsDir = new Dictionary<string, Bitmap>();
- private Yolo_Class yolo = new Yolo_Class();
- private int currDefectIndex = 0;
- private Color[] colorList = { Color.Red, Color.Green, Color.DarkViolet , Color.Magenta, Color.Orange, Color.Brown,
- Color.Olive, Color.PaleGreen, Color.CadetBlue,Color.Aqua,Color.YellowGreen,Color.Blue,
- Color.SpringGreen,Color.Fuchsia,Color.Wheat,Color.AliceBlue,Color.Azure };
- private string[] DefectCodes = { "dk", "zw","xws","gsyc","qk", "zk","pp","hs","yx", "xb", "sx","ds","gsdl" ,"cjdk"};
- #region 表格数据类型
- private class DefectStruct
- {
- //Defect_{order.SN}_{res.index}_X{res.Xmm}_Y{res.Ymm}
- public string Key { get; set; }
- public int CurrIndex { get; set; }//当前缺陷在本大图中的索引0-n
- public int MainIndex { get; set; }//大图
- public double MainX { get; set; }//大图X 大相机拍照时原始X,Y轴坐标
- public double MainY { get; set; }//大图X
-
- //["4","3.9","-0.8","dk","0.39"],["index","X","Y","缺陷类型","缺陷的置信度"]
- public int Index { get; set; }
- public double X { get; set; }
- public double Y { get; set; }
- public string DefectName { get; set; }//缺陷类型
- public double DefectCC { get; set; }//缺陷的置信度
-
- //2023-10-30 修复情况
- public string Restoratory { get; set; }
-
- public double Area { get; set; }
- }
- #endregion
-
- public DistributionFrm(Order order)
- {
- InitializeComponent();
- UIStyle.SetUIStyle(this);
- this.uiTitel1.FatherForm = this;
-
- _order = order;
-
- imageBox0.SetIconsVisible(false);
- imageBox0.ChangeCursorModeIsHand(true);
- //imageBox0.set
- imageBox1.SetIconsVisible(false);
- imageBox1.ChangeCursorModeIsHand(true);
- #region 图片列表设置
- this.dgvProcess.AutoGenerateColumns = false;
- //显示行号与列宽度自动调整
- dgvProcess.RowHeadersVisible = true;
- dgvProcess.RowHeadersWidth = 30;
- dgvProcess.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
- //dgvProcess.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
- dgvProcess.RowPostPaint += (sender, e) =>//序号列头
- {
- showRowNum_onDataGrid_RowPostPaint(this.dgvProcess, sender, e);
- };
-
- for (int i = 0; i < dgvProcess.Columns.Count; i++)//禁止点击列头排序
- dgvProcess.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
-
- dgvProcess.DefaultCellStyle.ForeColor = Color.Black;
- #endregion
- #region 图纸列表设置
- this.dataGridView1.AutoGenerateColumns = false;
- //显示行号与列宽度自动调整
- dataGridView1.RowHeadersVisible = true;
- dataGridView1.RowHeadersWidth = 30;
- dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
- //dgvProcess.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
- dataGridView1.RowPostPaint += (sender, e) =>//序号列头
- {
- showRowNum_onDataGrid_RowPostPaint(this.dataGridView1, sender, e);
- };
-
- for (int i = 0; i < dataGridView1.Columns.Count; i++)//禁止点击列头排序
- dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
-
- dataGridView1.DefaultCellStyle.ForeColor = Color.Black;
- #endregion
- //图片查看功能现阶段禁用,存在bug
- tabControl1.TabPages.Remove(this.tabPage1);
- }
-
- //数据表重绘
- private void showRowNum_onDataGrid_RowPostPaint(DataGridView dgv, object sender, DataGridViewRowPostPaintEventArgs e)
- {
- Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
- TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
- }
-
- #region 界面调整
- private void DistributionFrm_SizeChanged(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Maximized)
- {
- this.WindowState = FormWindowState.Normal;
- this.Top = 0;
- this.Left = 0;
- this.Width = SystemInformation.WorkingArea.Width;
- this.Height = SystemInformation.WorkingArea.Height;
- }
- }
- #endregion
-
- #region 获取缺陷代码对应缺陷项
- public string getDefectName(string defectCode)
- {
- defectCode = defectCode.ToLower();
- switch (defectCode)
- {
- case "dk":
- return "堵孔";
- case "zw":
- return "脏污";
- case "xws":
- return "纤维丝";
- case "gsyc":
- return "钢丝异常";
- case "qk":
- return "缺口";
- case "zk":
- return "针孔";
- case "pp":
- return "泡泡";
- case "hs":
- return "划伤";
- case "yx":
- return "压线";
- case "xb":
- return "斜边";
- case "sx":
- return "栅线";
- case "ds":
- return "断栅";
- default:
- return "未知";
-
- }
- }
- #endregion
-
- #region 数据获取
- private bool GetDefectDataFromSN(string sn, string date)
- {
- try
- {
- JObject data = new JObject();
- string path, key;
- string[] files;
-
- #region 获取所有缺陷数据
- //文件名格式:$"Size_SN{order.SN}_I{res.index}_X{res.Defects_X}_Y{res.Defects_Y}_.....json/bmp
- //--缺陷
- path = $"{ConfMgr.Instance.SysConfigParams.DefectRepairImag.SavePath}\\{date}\\{sn}\\";
- if (Directory.Exists(path))
- {
- files = Directory.GetFiles(path, $"Defect_SN{sn}_*.json", SearchOption.TopDirectoryOnly);
- //对文件名中的数字(index)进行大小排序
- files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
- foreach (string file in files)
- {
- key = Path.GetFileNameWithoutExtension(file);
- if (File.Exists(path + key + ".bmp"))
- data.Add(key, JArray.Parse(File.ReadAllText(file)));
- //加载小图
- //LoadDefectPic(new Bitmap(path + key + ".bmp"), key);
- }
- }
- //--缺陷加载小图
- path = $"{ConfMgr.Instance.SysConfigParams.DefectSmallImag.SavePath}\\{date}\\{sn}\\";
- if (Directory.Exists(path))
- {
- files = Directory.GetFiles(path, $"Defect_SN{sn}_*.bmp", SearchOption.TopDirectoryOnly);
- //对文件名中的数字(index)进行大小排序
- files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
- foreach (string file in files)
- {
- key = Path.GetFileNameWithoutExtension(file);
-
- //加载小图
- LoadDefectPic(new Bitmap(file), key);
- }
- }
- //--比对
- path = $"{ConfMgr.Instance.SysConfigParams.SizeRepairImag.SavePath}\\{date}\\{sn}\\";
- if (Directory.Exists(path))
- {
- files = Directory.GetFiles(path, $"Size_SN{sn}_*.json", SearchOption.TopDirectoryOnly);
- //对文件名中的数字(index)进行大小排序
- files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
- foreach (string file in files)
- {
- key = Path.GetFileNameWithoutExtension(file);
- if (File.Exists(path + key + ".bmp"))
- data.Add(key, JArray.Parse(File.ReadAllText(file)));
- }
- }
- #endregion
-
- #region 加载图片和数据
- string file_path;
- foreach (var kv in data)
- {
- defectInfoDir.Add(kv.Key, JsonConvert.DeserializeObject<List<List<string>>>(kv.Value.ToString()));
- file_path = $"{ConfMgr.Instance.SysConfigParams.DefectRepairImag.SavePath}\\{date}\\{sn}\\{kv.Key}.bmp";
- if (!File.Exists(file_path))
- throw new Exception(kv.Key + " 缺陷文件不存在!");
- Bitmap bmp = new Bitmap(file_path);
- defectBmpsDir.Add(kv.Key, bmp);
- }
- defectList.Clear();
- if (File.Exists(ConfMgr.Instance.SysConfigParams.ImageProcessDataPath))
- {
- double X, Y;
- int MainIndex;
- foreach (var item in defectInfoDir)
- {
- //Key : Defect_SN{order.SN}_I{res.index}_X{res.Xmm}_Y{res.Ymm}_C{res.defectCount}
- string[] str = item.Key.Split('_');
- MainIndex = Convert.ToInt32(str[2].Substring(1));
- X = Convert.ToDouble(str[3].Substring(1));
- Y = Convert.ToDouble(str[4].Substring(1));
-
- var arr = yolo.ImageXY2RepairTable(item.Value, X, Y, ConfMgr.Instance.SysConfigParams.ImageProcessDataPath);
-
- int i = 0;
- foreach (var arr2 in arr)
- {
- var obj = new DefectStruct();
- obj.Key = item.Key;
- obj.CurrIndex = i++;
- obj.MainIndex = MainIndex;
- obj.MainX = X;
- obj.MainY = Y;
- obj.Index = Convert.ToInt32(arr2[0].ToString());
- obj.X = Convert.ToDouble(arr2[1].ToString());
- obj.Y = Convert.ToDouble(arr2[2].ToString());
- obj.DefectName = getDefectName(arr2[3].ToString());
- obj.DefectCC = Convert.ToDouble(arr2[4].ToString());
- defectList.Add(obj);
- }
- }
-
- this.Invoke(new System.Action(() =>
- {
- this.dgvProcess.DataSource = null;
- //注:这样绑定List中的对象必需得用{get;set;}方式定义属性,否则单独格为空内容
- this.dgvProcess.DataSource = new BindingSource(defectList, null);
- this.gpbDefectList.Text = $"缺陷明细({defectList.Count}条)";
- }));
- gotoDefctListIndex(currDefectIndex);
- }
- else
- throw new Exception("转换文件不存在!Path:" + ConfMgr.Instance.SysConfigParams.ImageProcessDataPath);
- /*
- string file_path;
- bool first = true;
- foreach (var kv in data)
- {
- defectInfoDir.Add(kv.Key, JsonConvert.DeserializeObject<List<List<string>>>(kv.Value.ToString()));
-
- file_path = $"{ConfMgr.Instance.SysConfigParams.DefectRepairImag.SavePath}\\{date}\\{sn}\\{kv.Key}.bmp";
- if (!File.Exists(file_path))
- throw new Exception(kv.Key + " 缺陷文件不存在!");
- Bitmap bmp = new Bitmap(file_path);
- defectBmpsDir.Add(kv.Key, bmp);
-
- //["4","3.9","-0.8","dk","0.39"],["index","X","Y","缺陷类型","缺陷的置信度"]
- JArray arr = kv.Value as JArray;//二维数组
- foreach (JArray arr2 in arr)
- {
- var obj = new DefectStruct();
- obj.Key = kv.Key;
- obj.MainIndex = int.Parse(kv.Key.Split('_')[2].Substring(1));
- obj.Index = Convert.ToInt32(arr2[0].ToString());
- obj.X = Convert.ToDouble(arr2[1].ToString());
- obj.Y = Convert.ToDouble(arr2[2].ToString());
- obj.DefectName = getDefectName(arr2[3].ToString());
- obj.DefectCC = Convert.ToDouble(arr2[4].ToString());
- defectList.Add(obj);
- }
- if (first)
- {
- first = false;
- this.Invoke(new System.Action(() =>
- {
- this.reloadPic(bmp, defectInfoDir[defectList[0].Key], defectList[0]);
- //Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
- //mat = yolo.RepairTableDrawRec(defectInfor2RepairTable, defectInfo.MainX, defectInfo.MainY, defectInfo.CurrIndex, mat);
- }));
- }
- }
- this.Invoke(new System.Action(() =>
- {
- //注:这样绑定List中的对象必需得用{get;set;}方式定义属性,否则单独格为空内容
- this.dgvProcess.DataSource = new BindingSource(defectList, null);
- this.gpbDefectList.Text = $"缺陷明细({defectList.Count}条)";
- //2023-11-1 图纸对比缺陷标红
- for (int i = 0; i < defectList.Count; i++)
- {
- string defName = (string)this.dgvProcess.Rows[i].Cells["colDefectName"].Value;
- if ((defName == getDefectName("ds")) || ((defName == getDefectName("yx"))))
- this.dgvProcess.Rows[i].DefaultCellStyle.BackColor = Color.Red;
- }
- }));*/
- #endregion
-
-
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "加载错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return false;
- }
- return true;
- }
- #endregion
-
- #region 图片加载
- private void reloadPic(Bitmap bmp, List<List<string>> defectInfor2RepairTable, DefectStruct defectInfo)
- {
- //绘框
- if (defectInfor2RepairTable != null && defectInfo != null)
- {
- Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);
- mat = yolo.RepairTableDrawRec(defectInfor2RepairTable, defectInfo.MainX, defectInfo.MainY, defectInfo.CurrIndex, mat);
-
- //mat.ImWrite("ttt.bmp");
- imageBox0.RefreshWindow(mat);
- }
- }
- #endregion
-
- #region 表格事件
- private void dgvProcess_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
- {
- int preIndex = currDefectIndex;
- currDefectIndex = e.RowIndex;
- gotoDefctListIndex(currDefectIndex, preIndex);
- //图片对应不上,不使用
- //Control[] cts = this.pnlBmpList.Controls.Find("imgDefect_" + (currDefectIndex), true);
- //if((cts != null)&&(cts.Length>0))
- //{
- // cts[0].Select();
- //}
- }
- private void data_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
- {
- int preIndex = currDefectIndex;
- currDefectIndex = e.RowIndex;
- gotoDefctListIndex2(currDefectIndex, preIndex);
- //图片对应不上,不使用
- //Control[] cts = this.pnlBmpList.Controls.Find("imgDefect_" + (currDefectIndex), true);
- //if((cts != null)&&(cts.Length>0))
- //{
- // cts[0].Select();
- //}
-
- Control[] cts = this.flowLayoutPanel1.Controls.Find("imgDefect_" + (defectList[currDefectIndex].Index), true);
- if ((cts != null) && (cts.Length > 0))
- {
- cts[0].Select();
- }
- }
- #endregion
-
- #region 选择缺陷
- private void gotoDefctListIndex(int index, int preIndex = -1)
- {
- try
- {
- this.Invoke(new System.Action(() =>
- {
- string key = defectList[index].Key;
- if (defectBmpsDir.ContainsKey(key))
- {
- reloadPic(defectBmpsDir[key], defectInfoDir[key], defectList[index]);
- }
- if (preIndex > -1)
- this.dgvProcess.Rows[preIndex].DefaultCellStyle.BackColor = Color.White;
-
- this.dgvProcess.Rows[index].DefaultCellStyle.BackColor = Color.LightGreen;
- this.dgvProcess.Rows[index].Selected = true;
- //this.dgvProcess.CurrentCell = this.dgvProcess.Rows[index].Cells["colResult"];
- //this.dgvProcess.Rows[index].Cells["colResult"].Value = "已检";
-
- //reDrawByIndex(index);
- }));
- if ((defectList[index].X <= 0) || (defectList[index].Y <= 0))
- {
- return;
- }
-
- }
- catch (Exception ex)
- {
-
- }
- }
- private void gotoDefctListIndex2(int index, int preIndex = -1)
- {
- try
- {
- this.Invoke(new System.Action(() =>
- {
- if (preIndex > -1)
- this.dataGridView1.Rows[preIndex].DefaultCellStyle.BackColor = Color.White;
-
- this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.LightGreen;
- this.dataGridView1.Rows[index].Selected = true;
-
- reDrawByIndex(index);
- }));
- }
- catch (Exception ex)
- {
-
- }
- }
- #endregion
-
- #region 图纸显示
- private string _imgPath;
- private void reDraw(string imgPath)
- {
- _imgPath = imgPath;
- Bitmap bmp;
- Graphics g;
- using (System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath))
- {
- if (IsIndexedPixelFormat(img.PixelFormat))
- {
- bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
- g = Graphics.FromImage(bmp);
- g.DrawImage(img, 0, 0);
- }
- else
- {
- bmp = new Bitmap(imgPath);
- g = Graphics.FromImage(bmp);
- }
- }
-
- var list = _order.DefectInfoList;
- int x, y, w, h;
- int i = 0;
- int index = 0;
- foreach (string code in DefectCodes)
- {
- var lstDefectCode = list.Where(m => m.Code == code ).ToList();
- foreach (var info in lstDefectCode)
- {
- x = (int)info.X - 10;
- y = (int)info.Y - 10;
- w = h = 20;
- if (x < 0) x = 0;
- if (y < 0) y = 0;
- if (w > bmp.Width - 1) w = bmp.Width - 1;
- if (h > bmp.Height - 1) h = bmp.Height - 1;
- //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
- g.FillEllipse(new SolidBrush(colorList[i]), x, y, w, h);
-
- var obj = new DefectStruct();
- //obj.Key = info.Pid;
- obj.CurrIndex = index++;
- //obj.MainIndex = MainIndex;
- //obj.MainX = X;
- //obj.MainY = Y;
- //obj.Index = Convert.ToInt32(arr2[0].ToString());
- obj.X = info.X;
- obj.Y = info.Y;
- obj.Index = info.Index;
- obj.DefectName = getDefectName(info.Code);
- obj.DefectCC = info.ZXD;
- obj.Area = info.Area;
- defectList.Add(obj);
- }
- i++;
- }
-
- g.Flush();
- g.Dispose();
-
- this.Invoke(new System.Action(() =>
- {
- this.dataGridView1.DataSource = null;
- //注:这样绑定List中的对象必需得用{get;set;}方式定义属性,否则单独格为空内容
- this.dataGridView1.DataSource = new BindingSource(defectList, null);
- this.groupBox1.Text = $"缺陷明细({defectList.Count}条)";
- }));
- //
- //reloadPic(bmp);
- //ucImageView1.loadImage(bmp);
- OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);//用bitmap转换为mat
- imageBox1.RefreshWindow(mat);
- }
-
- private void reDrawByIndex(int index)
- {
- Bitmap bmp;
- Graphics g;
- using (System.Drawing.Image img = System.Drawing.Image.FromFile(_imgPath))
- {
- if (IsIndexedPixelFormat(img.PixelFormat))
- {
- bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
- g = Graphics.FromImage(bmp);
- g.DrawImage(img, 0, 0);
- }
- else
- {
- bmp = new Bitmap(_imgPath);
- g = Graphics.FromImage(bmp);
- }
- }
-
- var list = _order.DefectInfoList;
- int x, y, w, h;
- int i = 0;
- int t_index = 0;
- foreach (string code in DefectCodes)
- {
- var lstDefectCode = list.Where(m => m.Code == code).ToList();
- foreach (var info in lstDefectCode)
- {
- if (t_index == index)
- {
- x = (int)info.X - 20;
- y = (int)info.Y - 20;
- w = h = 40;
- if (x < 0) x = 0;
- if (y < 0) y = 0;
- if (w > bmp.Width - 1) w = bmp.Width - 1;
- if (h > bmp.Height - 1) h = bmp.Height - 1;
- //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
- g.FillRectangle(new SolidBrush(Color.LimeGreen), x, y, w, h);
- }
- else
- {
- x = (int)info.X - 10;
- y = (int)info.Y - 10;
- w = h = 20;
- if (x < 0) x = 0;
- if (y < 0) y = 0;
- if (w > bmp.Width - 1) w = bmp.Width - 1;
- if (h > bmp.Height - 1) h = bmp.Height - 1;
- //g.DrawRectangle(new Pen(item.ForeColor, 1.0f), x,y,w,h);
- g.FillEllipse(new SolidBrush(colorList[i]), x, y, w, h);
- }
- t_index++;
- }
- i++;
- }
-
- g.Flush();
- g.Dispose();
- //
- //reloadPic(bmp);
- //ucImageView1.loadImage(bmp);
- OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);//用bitmap转换为mat
- imageBox1.RefreshWindow(mat, ImageBox.ImageModeEnum.Part);
- }
- /// <summary>
- /// 判断图片是否索引像素格式,是否是引发异常的像素格式
- /// </summary>
- /// <param name="imagePixelFormat">图片的像素格式</param>
- /// <returns></returns>
- private bool IsIndexedPixelFormat(System.Drawing.Imaging.PixelFormat imagePixelFormat)
- {
- PixelFormat[] pixelFormatArray = {
- PixelFormat.Format1bppIndexed
- ,PixelFormat.Format4bppIndexed
- ,PixelFormat.Format8bppIndexed
- ,PixelFormat.Undefined
- ,PixelFormat.DontCare
- ,PixelFormat.Format16bppArgb1555
- ,PixelFormat.Format16bppGrayScale
- };
- foreach (PixelFormat pf in pixelFormatArray)
- {
- if (imagePixelFormat == pf)
- {
- return true;
- }
- }
- return false;
- }
- #endregion
-
- #region 界面加载
- private void DistributionFrm_Load(object sender, EventArgs e)
- {
- this.gboxDefectList.Tag = 0;
- this.pnlBmpList.Controls.Clear();
- this.flowLayoutPanel1.Controls.Clear();
-
- this.Cursor = Cursors.WaitCursor;
- #region 加载图纸缺陷查看
- Models.Attachment attachmentFile = _order.ProductInfo.AttachmentList.FirstOrDefault(x => x.Type == 0);
- string gbxBmpPath = "";
- if (attachmentFile != null)
- {
- gbxBmpPath = ConfMgr.Instance.ProjectDir + $"\\{attachmentFile.NameTimestamp}";
- if (!File.Exists(gbxBmpPath + ".bmp"))
- gbxBmpPath = "";
- }
- if (string.IsNullOrEmpty(gbxBmpPath))
- {
- MessageBox.Show("图纸文件不存在!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- //this.Close();
- }
- else
- {
- if (!File.Exists(gbxBmpPath + ".jpg"))//转背景后的图纸文件
- {
- //换背景JPG
- Mat mat = Cv2.ImRead(gbxBmpPath + ".bmp");
- Cv2.CvtColor(mat, mat, ColorConversionCodes.RGB2GRAY);//转灰度图
- for (int i = 0; i < mat.Height; i++)
- {
- for (int j = 0; j < mat.Width; j++)
- {
- if (mat.At<byte>(i, j) == 255)//白色
- mat.Set<byte>(i, j, 0);
- else
- mat.Set<byte>(i, j, 255);
- }
- }
-
- OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat).Save(gbxBmpPath + ".jpg", ImageFormat.Jpeg);
- }
- string imagePath = gbxBmpPath + ".jpg";
- reDraw(imagePath);
-
- //--缺陷加载小图
- string[] files;
- string key;
- string path = $"{ConfMgr.Instance.SysConfigParams.DefectSmallImag.SavePath}\\{_order.CreateTime.ToString("yyyMMdd")}\\{_order.SN}\\";
- if (Directory.Exists(path))
- {
- files = Directory.GetFiles(path, $"Defect_SN{_order.SN}_*.bmp", SearchOption.TopDirectoryOnly);
- //files = Directory.GetFiles(path, $"*.bmp", SearchOption.TopDirectoryOnly);
- //对文件名中的数字(index)进行大小排序
- //files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s))).ToArray();
- files = files.OrderBy(s => int.Parse(Path.GetFileNameWithoutExtension(s).Split('_')[2].Substring(1))).ToArray();
- foreach (string file in files)
- {
- key = Path.GetFileNameWithoutExtension(file).Split('_')[7];
- //MessageBox.Show($"{file}, {key}");
- //加载小图
- LoadDefectPic(new Bitmap(file), key);
- }
- }
- }
- #endregion
-
- #if false
- #region 加载大图缺陷查看
- if (!GetDefectDataFromSN(_order.SN, _order.CreateTime.ToString("yyyMMdd")))
- {
- //this.Close();
-
- }
- #endregion
- #endif
- pnlBmpList.VerticalScroll.Value = 0;
- flowLayoutPanel1.VerticalScroll.Value = 0;
- this.Cursor = Cursors.Default;
- }
- #endregion
-
- #region 加载小图
- private void LoadDefectPic(Bitmap bitmap, string name)
- {
- {
- //MessageBox.Show($"0");
- //加载缺陷小图
- //纵向显示
- int imgWidth = this.flowLayoutPanel1.ClientSize.Width - 50;
- //MessageBox.Show($"01-{this.flowLayoutPanel1.ClientSize.Width}");
- int imgHeight = (int)((bitmap.Height * 1.0f / bitmap.Width) * imgWidth + 0.5);
- //MessageBox.Show($"02 - {bitmap.Height} - {bitmap.Width}");
- int splitWidth = 20;
- //MessageBox.Show($"03");
- int pnlWidth = this.flowLayoutPanel1.ClientSize.Width;
- //MessageBox.Show($"04 - {this.flowLayoutPanel1.ClientSize.Width}");
- //int index = (int)this.groupBox2.Tag;
-
- //MessageBox.Show($"1 - {this.groupBox2.Tag}");
-
- PictureBox picbox = new PictureBox();
- picbox.Width = imgWidth;
- picbox.Height = imgHeight;
-
- picbox.Image = (Bitmap)bitmap.Clone();
- picbox.Name = "imgDefect_" + name;
- picbox.Tag = name;
-
- //MessageBox.Show($"2");
-
- picbox.Click += new EventHandler(defectBmpBox_Click);
- picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
- picbox.BorderStyle = BorderStyle.FixedSingle;
- //picbox.Margin = new Padding((pnlWidth - picbox.Height) / 2, splitWidth, 0, 0);
- picbox.Margin = new Padding((pnlWidth - picbox.Width) / 2, splitWidth, 0, 0);
- picbox.MouseHover += simpleTip_MouseHover;
- picbox.Visible = true;
-
- //MessageBox.Show($"3");
- this.flowLayoutPanel1.Controls.Add(picbox);
- flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.Maximum;
- //this.groupBox2.Tag = ++index;
- //this.groupBox2.Text = $"缺陷图像:{index} 张";
- //MessageBox.Show($"4");
- }
- }
-
- private void defectBmpBox_Click(object sender, EventArgs e)
- {
- //对应不上,不使用
- //PictureBox pb = sender as PictureBox;
- //int index = int.Parse(pb.Name.Replace("imgDefect_", ""));
- //dgvProcess_CellDoubleClick(null, new DataGridViewCellEventArgs(1, index));
-
- PictureBox pb = sender as PictureBox;
- int index = int.Parse(pb.Name.Replace("imgDefect_", ""));
- //MessageBox.Show($"{index}");
- for (int i = 0;i < defectList.Count; i++)
- {
- if (defectList[i].Index == index)
- {
- //MessageBox.Show($"111{index}");
- data_CellDoubleClick(null, new DataGridViewCellEventArgs(1, i));
- return;
- }
- }
-
- }
- private void simpleTip_MouseHover(object sender, EventArgs e)
- {
- PictureBox pb = sender as PictureBox;
- // 创建the ToolTip
- ToolTip toolTip1 = new ToolTip();
-
- // 设置显示样式
- toolTip1.AutoPopDelay = 5000;//提示信息的可见时间
- toolTip1.InitialDelay = 500;//事件触发多久后出现提示
- toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框
- toolTip1.ShowAlways = true;//是否显示提示框
-
- // 设置伴随的对象.
- toolTip1.SetToolTip(pb, pb.Name + "[" + pb.Tag.ToString() + "]");//设置提示按钮和提示内容
- }
- #endregion
- }
- }
|