Exemple #1
0
        /// <summary>
        /// 截图和识别功能
        /// </summary>
        /// <param name="flag">true根据坐标截图到桌面,false不生成</param>
        private void changeLabelText(bool flag)
        {
            PICResponse response = JsonConvert.DeserializeObject <PICResponse>(image_ecognition.SaveImg(flag));

            if (response.msg == "读取坐标失败!")
            {
                MessageBox.Show("请设置坐标!");
                //取消执行
                timer.Dispose();
                timebtn.Text = "启动";
            }
            if (this.IsHandleCreated)
            {
                floatFormNew.FloatLabel.BeginInvoke(new Action(() =>
                {
                    if (string.IsNullOrEmpty(response.data))
                    {
                        floatFormNew.FloatLabel.Text = "金额:0";
                    }
                    else
                    {
                        floatFormNew.FloatLabel.Text = "金额:" + response.data;
                    }
                }), null);
            }
        }
Exemple #2
0
        public string SaveImg(Boolean flag = false)
        {
            // 如果初始化失败重新初始化一次
            if (ReadZB.point.Count == 0)
            {
                // 初始化坐标数据
                ReadZB zb = new ReadZB();
                zb.readpoint();
            }
            string ret = string.Empty;

            try
            {
                using (Bitmap myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
                {
                    Graphics g = Graphics.FromImage(myImage);

                    g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
                    g.ReleaseHdc(g.GetHdc());
                    g.Dispose();

                    int i = 0;
                    #region 获取坐标
                    if (ReadZB.point.Count > 0)
                    {
                        foreach (string line in ReadZB.point)
                        {
                            i++;
                            rectX  = Convert.ToInt32(line.Split(',')[0]);
                            rectY  = Convert.ToInt32(line.Split(',')[1]);
                            width  = Convert.ToInt32(line.Split(',')[2]);
                            height = Convert.ToInt32(line.Split(',')[3]);

                            // 保存图片到图片框
                            Bitmap   bmp     = new Bitmap(width, height);
                            Graphics partPic = Graphics.FromImage(bmp);
                            partPic.DrawImage(myImage, new Rectangle(0, 0, width, height), new Rectangle(rectX, rectY, width, height), GraphicsUnit.Pixel);

                            if (flag)
                            {
                                bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + DateTime.Now.ToFileTime().ToString() + ".png");
                            }


                            MemoryStream ms = new MemoryStream();
                            bmp.Save(ms, ImageFormat.Png);
                            byte[] picdata = ms.GetBuffer(); //StreamToBytes(ms);
                                                             //BytesToImage(picdata);
                            string response = CreatePostData(ReadZB._URL, DateTime.Now.ToFileTime().ToString(), picdata);


                            PICResponse pir = JsonConvert.DeserializeObject <PICResponse>(response);
                            ms.Close();
                            partPic.Dispose();
                            bmp.Dispose();
                            bmp = null;
                            try
                            {
                                if (!string.IsNullOrEmpty(pir.data) && decimal.Parse(pir.data) != 0)//判断是否为空
                                {
                                    ret = JsonConvert.SerializeObject(pir);
                                    break;                                                     //跳出循环
                                }
                                if (i == ReadZB.point.Count && string.IsNullOrEmpty(pir.data)) //如果是最后一个坐标并且还没数据
                                {
                                    ret = "{\"msg\":\"没获取到坐标中的数据!\",\"code\":500,\"data\":\"\"}";
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Save(ex.ToString());
                                continue;//继续循环
                            }
                        }
                    }
                    else
                    {
                        ret = "{\"msg\":\"读取坐标失败!\",\"code\":500,\"data\":\"\"}";
                    }
                    if (string.IsNullOrEmpty(ret))
                    {
                        ret = "{\"msg\":\"未读取到数据!\",\"code\":500,\"data\":\"\"}";
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                SystemMemeoryCleanup.ClearMemory();//清理缓存(类似360加速球的效果)
                Log.Save(ex.ToString());
                ret = "{\"msg\":\"出现异常,请联系管理员!\",\"code\":500,\"data\":\"\"}";
            }
            return(ret);
        }