Exemplo n.º 1
0
 /// <summary>
 /// 发送图像到远程
 /// </summary>
 private void SendImage()
 {
     Task.Factory.StartNew(() =>
     {
         Rectangle rectangle = new Rectangle(0, 0, Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
         while (!_isClose)
         {
             if (_isStartCapture)
             {
                 using (Bitmap desktopImage = new Bitmap(rectangle.Width, rectangle.Height))
                 {
                     using (Graphics g = Graphics.FromImage(desktopImage))
                     {
                         g.CopyFromScreen(0, 0, 0, 0, desktopImage.Size);
                         MouseAndKeyHelper.DrawMouse(g);
                         using (MemoryStream ms = ImageHelper.GetLossyCompression(desktopImage, (int)_qualityLevel, "W", 800))
                         {
                             if (desktopImage != null)
                             {
                                 try
                                 {
                                     ms.Position = 0;
                                     _mCleint.SendFileAsync(_remote, ms.GetBuffer());
                                     var sec = 1000;
                                     if (_fps > 0)
                                     {
                                         sec = 1000 / _fps;
                                     }
                                     else
                                     {
                                         sec = 2000;
                                     }
                                     Thread.Sleep(sec);
                                 }
                                 catch
                                 {
                                 }
                                 finally
                                 {
                                     ms.Dispose();
                                     g.Dispose();
                                     desktopImage.Dispose();
                                 }
                             }
                         }
                     }
                 }
             }
             else
             {
                 Thread.Sleep(100);
             }
         }
     });
 }
Exemplo n.º 2
0
 /// <summary>
 /// 每5秒发送一张整图防错
 /// </summary>
 public void SendImageKeep()
 {
     Task.Factory.StartNew(() => {
         Rectangle rectangle = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
         while (!_isClose)
         {
             try
             {
                 if (true)
                 {
                     using (Bitmap desktopImage = new Bitmap(rectangle.Width, rectangle.Height))
                     {
                         using (Graphics g = Graphics.FromImage(desktopImage))
                         {
                             try
                             {
                                 g.CopyFromScreen(0, 0, 0, 0, desktopImage.Size);
                             }
                             catch (Exception ex)
                             {
                                 continue;
                             }
                             MouseAndKeyHelper.DrawMouse(g);
                             SendImageFile(desktopImage, g);
                             Thread.Sleep(secKeep);
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 continue;
             }
         }
     }, cts.Token);
 }
Exemplo n.º 3
0
 /// <summary>
 /// 发送图像到远程
 /// </summary>
 private void SendImage()
 {
     Task.Factory.StartNew(() =>
     {
         //Bitmap lastBitmap = null;
         Rectangle rectangle = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
         while (!_isClose)
         {
             try
             {
                 if (true)
                 {
                     using (Bitmap desktopImage = new Bitmap(rectangle.Width, rectangle.Height))
                     {
                         using (Graphics g = Graphics.FromImage(desktopImage))
                         {
                             try
                             {
                                 g.CopyFromScreen(0, 0, 0, 0, desktopImage.Size);
                             }
                             catch (Exception ex)
                             {
                                 continue;
                             }
                             MouseAndKeyHelper.DrawMouse(g);
                             //比较此次截图与上一张截图的差异
                             if (lastBitmap != null)
                             {
                                 List <Rectangle> rects = ImageComparer.Compare(lastBitmap, desktopImage);
                                 lastBitmap             = (Bitmap)desktopImage.Clone();
                                 if (rects.Count == 0)//无变化不发送
                                 {
                                     Thread.Sleep(sec);
                                 }
                                 if (rects.Count <= 480)//差异小于7块则分段传输
                                 {
                                     Dictionary <Rectangle, Bitmap> dic = new Dictionary <Rectangle, Bitmap>();
                                     foreach (var rect in rects)
                                     {
                                         Bitmap bmSmall = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppRgb);
                                         using (Graphics grSmall = Graphics.FromImage(bmSmall))
                                         {
                                             grSmall.DrawImage(desktopImage, 0, 0,
                                                               rect, GraphicsUnit.Pixel);
                                             grSmall.Save();
                                             grSmall.Dispose();
                                         }
                                         dic.Add(rect, (Bitmap)bmSmall.Clone());
                                     }
                                     if (dic.Count > 0)
                                     {
                                         _mCleint.SendFileSlice(_remote, CompressHelper.Compress(SerializeHelper.ByteSerialize(dic)));
                                         isBigChnage = false;
                                         Thread.Sleep(sec);
                                     }
                                 }
                                 else
                                 {//发送完整的图片
                                     if (isBigChnage == false)
                                     {
                                         SendImageFile(desktopImage, g);
                                         lastBitmap = (Bitmap)desktopImage.Clone();//要在之前否则销毁掉了
                                         Thread.Sleep(sec);
                                     }
                                     else
                                     {
                                         isBigChnage = false;
                                     }
                                 }
                             }
                             else
                             {                                              //第一张图片
                                 lastBitmap = (Bitmap)desktopImage.Clone(); //要在之前否则销毁掉了
                                 SendImageFile(desktopImage, g);
                                 Thread.Sleep(sec);
                             }
                         }
                     }
                 }
                 else
                 {
                     Thread.Sleep(100);
                 }
             }
             catch (Exception ex)
             {
                 continue;
             }
         }
     }, cts.Token);
 }