using Devart.Common; using MaiMuAOI.SysCtrl; using Models; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MaiMuAOI.SysUI.DefectPicShow { public partial class DefectImageShowFrm : Form { 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 Order mOrder; private string imgPath; private int defectType; /// /// 0-Defect 1-Size /// /// /// /// 0-Defect 1-Size public DefectImageShowFrm(string _imgPath, Order order, int _defectType = 0) { InitializeComponent(); UIStyle.SetUIStyle(this); this.uiTitel1.FatherForm = this; mOrder = order; imgPath = _imgPath; defectType = _defectType; if (defectType == 0) { this.Text = "缺陷分布图"; } else { this.Text = "比对失败区域分布"; } } private void initDefectMenus() { if (mOrder == null || mOrder.DefectInfoList == null) throw new Exception("记录项为空!"); tsbtnDefectList.DropDownItems.Clear(); //全缺陷项 var lstDefect = ConfMgr.GetArrayList(); foreach (DictionaryEntry item in lstDefect) { string code = item.Value.ToString();//zx item.Key=0/1/2/3 int key = int.Parse(item.Key.ToString()); string name = ((DefectNameEnum)key).ToString(); int num = mOrder.DefectInfoList.Where(x => x.Code == code && x.Type == defectType).Count();//缺陷项个数 ToolStripMenuItem menuItem = new ToolStripMenuItem() { Name = $"btnSub_{code}", Tag = code, Text = $"{name}({num})", ForeColor = colorList[key], Checked = true }; tsbtnDefectList.DropDownItems.Add(menuItem); menuItem.Click += (object sender, EventArgs e) => { menuItem.Checked = !menuItem.Checked; //if(menuItem.num>0) reDraw(); }; } } private void FrmShowSizeTag_Load(object sender, EventArgs e) { try { initDefectMenus(); reDraw(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void tsbtnRefesh_Click(object sender, EventArgs e) { try { initDefectMenus(); reDraw(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void tsbtnClose_Click(object sender, EventArgs e) { this.Close(); } private void reDraw() { 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 = mOrder.DefectInfoList; string code; int x, y, w, h; foreach (ToolStripMenuItem item in tsbtnDefectList.DropDownItems) { if (!item.Checked) continue; code = item.Tag.ToString(); var lstDefectCode = list.Where(m => m.Code == code && m.Type == defectType).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(item.ForeColor), x, y, w, h); } } g.Flush(); g.Dispose(); // //reloadPic(bmp); //ucImageView1.loadImage(bmp); OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bmp);//用bitmap转换为mat imageBox1.RefreshWindow(mat); } /// /// 判断图片是否索引像素格式,是否是引发异常的像素格式 /// /// 图片的像素格式 /// 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; } } }