Пример #1
0
        /// <summary>
        /// 初始化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            // 初始化坐标数据
            ReadZB zb = new ReadZB();

            zb.readpoint();

            label1.Text   = "设置坐标个数:" + ReadZB.point.Count;
            Interval      = ConfigurationManager.AppSettings["Time"];
            timetext.Text = Interval;
            if (ReadZB.point.Count > 0)
            {
                foreach (string line in ReadZB.point)
                {
                    textBox1.Text += line + "\r\n";
                }
            }


            floatFormNew.Show();

            bool isStartService = bool.Parse(config.AppSettings.Settings["isStartService"].Value);

            if (isStartService)
            {
                //最小化窗口
                WindowState = FormWindowState.Minimized;
                Thread.Sleep(200);
                timer        = new System.Threading.Timer(new TimerCallback(timer_Elapsed), this, 0, Convert.ToInt32(Interval));
                timebtn.Text = "停止";
            }
        }
Пример #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);
        }