FrmAoiSetting.cs 11.1 KB
using AOI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccAOI
{
    public partial class FrmAoiSetting : Asa.Theme.FlatForm
    {
        private control.ABaseControl aoiControl = null;
        public Image Img = null;
        private AoiProject Project = null;
        public FrmAoiSetting()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }
        public FrmAoiSetting(string programPath, Image image = null)
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            if (!programPath.Equals("") && (image != null))
            {
                Project = new AoiProject(image);
                Project.Load(programPath);
            }

            ShowPorject();
        }
        private void FrmAoiSetting_Load(object sender, EventArgs e)
        {
            comType.ItemAdd("Mark点设置");
            comType.ItemAdd("斑点分析");
            comType.ItemAdd("颜色抽取");
            comType.Text = "Mark点设置";
        }

        private void btnOpenImage_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
            openDialog.Title = "打开本地图片";
            openDialog.Filter = "All Supported Images (*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png)|*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
            openDialog.DefaultExt = "png";
            //openDialog.DefaultExt = "png";
            System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            string fileName = openDialog.FileName;

            //读取图片内容
            this.Img = (Image)Image.FromFile(fileName).Clone();
            imageBox1.Image = Img;
        }

        private void btnGetCameraImg_Click(object sender, EventArgs e)
        {
            string camera = cmbCameraList.Text;
            if (camera.Equals(""))
            {
                MessageBox.Show("清先选择相机");
                return;
            }
        }

        private void btnOpenPro_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
            openDialog.Title = "打开项目";
            openDialog.Filter = "(*.data)|*.data|(*.*)|*.*";
            //openDialog.DefaultExt = "png";
            System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            string fileName = openDialog.FileName;
            Project = new AoiProject(this.Img);
            Project.Load(fileName);

            ShowPorject();
        }

        private void btnSavePro_Click(object sender, EventArgs e)
        {
            if (this.Project == null)
            {
                MessageBox.Show("没有项目可保存");
                return;
            }
            SaveCurrAoi();
            System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
            saveFileDialog.Title = "保存项目";
            saveFileDialog.Filter = "(*.data)|*.data|(*.*)|*.*";
            //openDialog.DefaultExt = "png";
            System.Windows.Forms.DialogResult result = saveFileDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            string fileName = saveFileDialog.FileName;
            Project.Save(fileName);
            MessageBox.Show("保存成功");
        }

        private void btnNewAoi_Click(object sender, EventArgs e)
        {
            if (this.Img == null)
            {
                MessageBox.Show("清先打开图片");
                return;
            }
            if (this.Project == null)
            {
                Project = new AoiProject(this.Img);
            }
            if (comType.Text.Equals(""))
            {
                MessageBox.Show("请选择类型");
                comType.Focus();
                return;
            }
            string text = comType.Text;
            CloseCurrAoi();
            AoiMethod methodInfo = null;
            if (text.Equals("Mark点设置"))
            {
                methodInfo = new AoiMarkMethod();  

            }
            else if (text.Equals("斑点分析"))
            {
                methodInfo = new AoiBlobMethod(); 
            }
            else
            {
                methodInfo = new AoiMethodRgb(); 
            }
            methodInfo.MethodName = Project.methodMap.Values.Count.ToString().PadLeft(2, '0') + "_" + text;

            Project.methodMap.Add(methodInfo.MethodName, methodInfo);
            ShowAoi(methodInfo);
            aoiList.ItemAdd(aoiControl.TitleName);
        }
        private void ShowAoi(AoiMethod method)
        {
            if (aoiControl == null)
            {
                if(method is AoiBlobMethod)
                {
                    aoiControl = new control.AoiBlobControl();
                }else if(method is AoiMarkMethod)
                {
                    aoiControl = new control.AioMarkControl();
                }else if(method is AoiMethodRgb)
                {
                    aoiControl = new control.AoiRgbControl();
                }
            }
            this.aoiControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
         | System.Windows.Forms.AnchorStyles.Left)
         | System.Windows.Forms.AnchorStyles.Right)));
            aoiControl.FormBorderStyle = FormBorderStyle.None;
            aoiControl.StartPosition = FormStartPosition.CenterParent;
            aoiControl.Location = new Point(0, 0);
            aoiControl.BImageBox=this.imageBox1;
            aoiControl.AoiInfo = method;
            aoiControl.Size = panAoi.Size;
            aoiControl.TitleName = method.MethodName;
            aoiControl.TopLevel = false;
            aoiControl.Parent = panAoi;
            aoiControl.Show();
            aoiControl.ShowAoiInfo();

            if (method.RoiPath != null)
            {
                currPath = method.RoiPath;
                imageBox1.SelectionRegion = method.RoiPath;
                Image threshImage = CutImage(imageBox1.Image, currPath);
                cutImage = threshImage;

                aoiControl.currPath = currPath;
                aoiControl.SetImage( threshImage);
            }
        }
        private void SaveCurrAoi()
        {
            if (aoiControl != null &&aoiControl.Visible)
            {
                AoiMethod method = aoiControl.GetAoiInfo();

                //更新
                if (Project.methodMap.ContainsKey(method.MethodName))
                {
                    Project.methodMap[method.MethodName] = method;
                }
            }
        }
        private void CloseCurrAoi()
        {
            SaveCurrAoi();
            if (aoiControl != null)
            {
                if (aoiControl.Visible)
                { 
                    aoiControl.Close();
                }
                aoiControl.Dispose();
                aoiControl = null;
            }
        }
        private void ShowPorject()
        {
            if (Project != null)
            {
                aoiList.ItemClear();
                imageBox1.Image = Project.standardImage;
                imageBox1.SelectionRegion = new GraphicsPath();
                if (Project.methodMap.Count > 0)
                { 
                    foreach(string key in Project.methodMap.Keys)
                    {
                        aoiList.ItemAdd(key);
                    }
                    aoiList.Index = 0;
                }
            }
        }

        private void aoiList_IndexChanged(object sender, EventArgs e)
        {
            int index = aoiList.Index;
            if (index >= 0 && (Project != null))
            {
                List<AoiMethod> methods = new List<AoiMethod>(Project.methodMap.Values);
                if (index < methods.Count)
                {
                    AoiMethod method = methods[index];
                    if (aoiControl != null && aoiControl.TitleName.Equals(method.MethodName))
                    {
                        return;
                    }
                    else
                    {
                        CloseCurrAoi();
                        ShowAoi(method);
                    }
                }
            }
        }

        private Image cutImage = null;
        private GraphicsPath currPath = null;
        private void imageBox1_SelectionRegionChanged(object sender, EventArgs e)
        {
            try
            {
                if (imageBox1.Image == null || this.Img == null || aoiControl == null)
                {
                    return;
                }
                Matrix translateMatrix = new Matrix();
                translateMatrix.Translate(100, 0);

                //RectangleF region = imageBox1.SelectionRegion;
                //currPath = new GraphicsPath();
                //if (aoiControl.AreaType.Equals(1))
                //{
                //    currPath.AddRectangle(region);
                //}
                //else
                //{
                //    currPath.AddEllipse(region);
                //}
                currPath = imageBox1.SelectionRegion;
                Image threshImage = CutImage(imageBox1.Image, currPath);
                cutImage = threshImage;

                aoiControl.currPath = currPath;
                aoiControl.SetImage( threshImage);
            }catch(Exception ex)
            {
                
            } 
        }
        private Image CutImage(Image src, GraphicsPath path)
        {
            var bounds = path.GetBounds();
            Bitmap b = new Bitmap(src.Width, src.Height);
            using (Graphics g = Graphics.FromImage(b))
            {
                var br = new TextureBrush(src);
                g.FillPath(br, path);
            }
            if (bounds.Width > 0 && bounds.Height > 0)
            {
                Bitmap result = new Bitmap((int)bounds.Width, (int)bounds.Height);
                var srcRect = bounds;
                var dstRect = new RectangleF(0, 0, bounds.Width, bounds.Height);
                bounds.Location = System.Drawing.Point.Empty;
                using (Graphics g = Graphics.FromImage(result))
                {
                    bounds.Location = System.Drawing.Point.Empty;
                    g.DrawImage(b, dstRect, srcRect, GraphicsUnit.Pixel);
                }
                return result;
            }
            return null;

        }
    }
}