MainForm.cs 6.6 KB

using Acc.Img;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Acc.Demo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

       
        private void OpenImage()
        {
            using (FileDialog dialog = new OpenFileDialog())
            {
                dialog.Filter = "图片 (*.raw,*.bmp;*.gif;*.jpeg;*.jpg;*.png)|*.raw;*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|RAW (*.raw)|*.raw|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
                dialog.DefaultExt = "png";

                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    try
                    {
                        string imagePath = dialog.FileName;
                        labelImageName.Text = imagePath;
                        Image image = ImageUtil.ReadImage(imagePath);
                        orginalImage = image;
                        imageBox.Image = image;
                        imageBox.ZoomToFit();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
        

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            ClearStatusBar();
            OpenImage();
        }

        Image orginalImage = null;
        private void buttonCount_Click(object sender, EventArgs e)
        {
            Count();
        }

        private void buttonThesh_Click(object sender, EventArgs e)
        {
            int theshValue = -1;
            if (!checkBoxAutoThresh.Checked)
            {
                theshValue = int.Parse(textBoxThesh.Text);
            }
            Image result;
            result = ImageUtil.Threshhold(orginalImage, theshValue);
            //if (theshValue == -1)
            //{
            //    result = ImageUtil.Threshhold(imageBox.Image, theshValue);
            //}
            //else
            //{
            //    result = ImageUtil.DistanceTransform(imageBox.Image);
            //}
            imageBox.Image = result;
        }
        
        private void checkBoxAutoThresh_CheckedChanged(object sender, EventArgs e)
        {
            textBoxThesh.Enabled = !checkBoxAutoThresh.Checked;
        }

        private void 原图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearStatusBar();
            imageBox.Image = orginalImage;
        }

        private static int markX = -1;
        private static int markY = -1;

        private void imageBox_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Right)
            {
                bool inImage = imageBox.IsPointInImage(e.Location);
                System.Drawing.Point markPoint = imageBox.PointToImage(e.Location);
                markX = markPoint.X;
                markY = markPoint.Y;
                元件特征ToolStripMenuItem.Enabled = inImage;
                原图ToolStripMenuItem.Enabled = inImage;
                contextMenuStrip.Show(this, e.Location);
            }
        }

        private void Count()
        {
            ClearStatusBar();
            Task.Factory.StartNew(delegate {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                int itemFeature = int.Parse(textBoxFeature.Text);
                Image image = orginalImage;
                int count = ImageUtil.CountItems(ref image, itemFeature);
                sw.Stop();
                labelTime.Text = "耗时:" + sw.ElapsedMilliseconds + " ms";
                labelCount.Text = "数量:" + count;
                imageBox.Image = image;
            });
        }
        private void ClearStatusBar()
        {
            labelFeature.Text = "";
            labelCount.Text = "";
            labelTime.Text = "";
        }

        private void 打开图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearStatusBar();
            OpenImage();
        }

        private void 计数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Count();
        }

        private void 元件特征ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearStatusBar();
            Image image = orginalImage;
            //int feature = ImageUtil.GetFeature(ref image, markX, markY);
            int feature = ImageUtil.GetGrayValue(ref image, markX, markY);
            textBoxFeature.Text = feature.ToString();
            imageBox.Image = image;

            //int theshValue = -1;
            //if (!checkBoxAutoThresh.Checked)
            //{
            //    theshValue = int.Parse(textBoxThesh.Text);
            //}
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            //Image image = ImageUtil.Mark(orginalImage, markX, markY, theshValue);

            //imageBox.Image = image;
            //sw.Stop();
            //Console.WriteLine("总耗时:" + sw.ElapsedMilliseconds + " ms");
        }

        private void 保存当前图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (FileDialog dialog = new SaveFileDialog())
            {
                dialog.Filter = "图片 (*.raw,*.bmp;*.gif;*.jpeg;*.jpg;*.png)|*.raw;*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|RAW (*.raw)|*.raw|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
                dialog.DefaultExt = "png";
                //保存对话框是否记忆上次打开的目录 
                dialog.RestoreDirectory = true;

                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    try
                    {
                        string imagePath = dialog.FileName;

                        imageBox.Image.Save(imagePath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
    }
}