コード例 #1
0
ファイル: Form1.cs プロジェクト: LanTenggit/MESMarkingMachine
        /// <summary>
        /// 获取MES状态
        /// </summary>
        public void MesState()
        {
            //获得当前屏幕的分辨率
            Screen    scr     = Screen.PrimaryScreen;
            Rectangle rc      = scr.Bounds;
            int       iWidth  = rc.Width;
            int       iHeight = rc.Height;
            //创建一个和屏幕一样大的Bitmap
            Image myImage = new Bitmap(iWidth, iHeight - 50);
            //Image myImage = new Bitmap(688, 520);
            //从一个继承自Image类的对象中创建Graphics对象
            Graphics g        = Graphics.FromImage(myImage);
            string   jietustr = string.Empty;

            while (true)
            {
                try
                {
                    Thread.Sleep(100);
                    if (serial.IsOpen)
                    {
                        this.Invoke(new Action(() => {
                            logNum = Convert.ToInt32(tb_LogNum.Text);
                        }));
                        string imgpath = Path.GetFullPath("images");
                        imgpath += "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";

                        //抓屏并拷贝到myimage里
                        g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
                        //保存为文件
                        myImage.Save(imgpath);

                        //string Contrast_picturePath = imgpath;
                        //byte[] JieTuByte = DataFunction.GetImageByte(Contrast_picturePath);
                        jietustr = DataFunction.GetBase64StringByImage(myImage);
                        //myImage.Dispose();
                        if (jietustr == SaveImgstr)
                        {
                            string Write = "相同图片";
                        }
                        else
                        {
                            SaveImgstr = jietustr;
                            string Writestr = string.Empty;
                            string Runstate = string.Empty;
                            this.Invoke(new Action(() =>
                            {
                                if (IsMesState(imgpath))
                                {
                                    logNum++;
                                    this.Invoke(new Action(() =>
                                    {
                                        Writestr       = tb_OK.Text;
                                        tb_LogNum.Text = logNum.ToString();
                                    }));
                                }
                                else
                                {
                                    this.Invoke(new Action(() => {
                                        Writestr = tb_NG.Text;
                                    }));
                                }
                            }));

                            Runstate = Writestr;

                            byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(Writestr);
                            Writestr = HexStringTenByte.ByteArrayToHexString(byteArray);
                            byte[] buff = HexStringTenByte.HexString2Bytes(Writestr);
                            serial.Write(buff, 0, buff.Length);
                            this.Invoke(new Action(() =>
                            {
                                this.lb_send.Text = "Send:" + DateTime.Now.ToString("HH:mm:ss");
                                this.tb_send.Text = Runstate;
                            }));
                        }
                        string ImgPath = Path.GetFullPath("images");
                        int    FileNum = DataFunction.GetFileNum(ImgPath);
                        if (FileNum >= 10)
                        {
                            DataFunction.DelectDir(ImgPath);
                        }
                        GC.Collect();
                        //myImage.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    DataFunction.txtWrite(ex.ToString());
                }
                GC.Collect();
            }
        }