您的位置:程序门 -> .net技术 -> c#



如何使屏幕捕捉的显示速度加快,内附代码??


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何使屏幕捕捉的显示速度加快,内附代码??
发表于:2007-10-26 10:14:56 楼主
我在做一个屏幕捕捉程序,像qq那样的
完成运行,被捕捉的机器的速度奇慢无比,查看方的显示的当然也是慢的,有什么方法可以降低cpu使用率或着修改哪些地方可以提高性能?
代码:
using   system;
using   system.collections.generic;
using   system.componentmodel;
using   system.data;
using   system.drawing;
using   system.text;
using   system.windows.forms;
using   system.net.sockets;
using   system.threading;
using   system.net;
using   system.io;
namespace   thclient
{
          public   partial   class   form1   :   form
          {
          //           private   socket   socket   =   null;
                  private   networkstream   ns   =   null;
                  private   streamreader   sr   =   null;
                  private   streamwriter   sw   =   null;
                  private   thread   tcpthread   =   null;
                  private   tcpclient   tcpclient   =   null;
                  memorystream   ms   =   null;
                  bool   iscon   =   false;
                  bool   iserror   =   false;
                 
                 
                  public   form1()
                  {
                          initializecomponent();
             
                  }
             
         
                  [system.runtime.interopservices.dllimport("gdi32.dll")]
                  private   static   extern   intptr   createdc(
                              string   lpszdriver,   //   驱动名称
                              string   lpszdevice,   //   设备名称
                              string   lpszoutput,   //   无用,可以设定位"null"
                              intptr   lpinitdata   //   任意的打印机数据
                          );
                  [system.runtime.interopservices.dllimportattribute("gdi32.dll")]
                  private   static   extern   bool   bitblt(
                  intptr   hdcdest,   //目标设备的句柄
                  int   nxdest,   //   目标对象的左上角的x坐标
                  int   nydest,   //   目标对象的左上角的x坐标
                  int   nwidth,   //   目标对象的矩形的宽度
                  int   nheight,   //   目标对象的矩形的长度
                  intptr   hdcsrc,   //   源设备的句柄
                  int   nxsrc,   //   源对象的左上角的x坐标
                  int   nysrc,   //   源对象的左上角的x坐标
                  system.int32   dwrop   //   光栅的操作值
                  );
                  public   void   capture()
                  {
                          //this.visible   =   false;
                          intptr   dc1   =   createdc("display",   null,   null,   (intptr)null);
                          //创建显示器的dc
                          graphics   g1   =   graphics.fromhdc(dc1);
                          //由一个指定设备的句柄创建一个新的graphics对象
                          system.drawing.image   myimage   =   new   bitmap(screen.primaryscreen.bounds.width,  

screen.primaryscreen.bounds.height,   g1);
                          //根据屏幕大小创建一个与之相同大小的bitmap对象
                          graphics   g2   =   graphics.fromimage(myimage);
                          //获得屏幕的句柄
                          intptr   dc3   =   g1.gethdc();
                          //获得位图的句柄
                          intptr   dc2   =   g2.gethdc();
                          //把当前屏幕捕获到位图对象中
                          bitblt(dc2,   0,   0,   screen.primaryscreen.bounds.width,   screen.primaryscreen.bounds.height,   dc3,   0,   0,  

13369376);
                          //把当前屏幕拷贝到位图中
                          g1.releasehdc(dc3);
                          //释放屏幕句柄
                          g2.releasehdc(dc2);
                          //释放位图句柄
                          ms   =   new   memorystream();
                          myimage.save(ms,system.drawing.imaging.imageformat.jpeg);
                          byte[]   b   =ms.getbuffer();
                          ns.write(b,   0,   b.length);
                          ms.flush();
                        //   this.picturebox1.image   =   image.fromstream(ms);
                          //this.visible   =   true;
                         
                     
                  }
                  private   void   form1_load(object   sender,   eventargs   e)
                  {
                          this.label1.text   =   "程序启动中....\r\n";
                          timer1.enabled   =   true;
                  }
                  public   void   connect()
                  {
                            try
                          {
                                 
                                    while   (true)
                                  {
                                        capture();
                                  }
                             
                          }
                          catch(exception   ex)
                          {
                              //       messagebox.show(ex.message);
                              //       messagebox.show("错误!===\\\\"+ex.message   );
                                  sw.dispose();
                                  sr.dispose();
                                  ns.dispose();
                                  iscon   =   false;
                                  iserror   =   false;
                                  timer1.enabled   =   true;
                                  this.label1.text   +=   "远程主机被断开~!";
                          }
                  }
                  //关闭所有流
                  private   void   form1_closing(object   sender,system.componentmodel.canceleventargs   e)
                  {
                     
                  }
                  private   void   timer1_tick(object   sender,   eventargs   e)
                  {
                          if   (!iscon)
                          {
                                con();
                                if   (!iserror)
                                {
                                  timer1.enabled   =   false;
                                tcpthread   =   new   thread(new   threadstart(connect));
                                tcpthread.start();
                             
                                }
                                iserror   =   false;
                          }
                  }
                  public   void   con()
                  {
                          try
                          {
                                  this.label1.text   +=   "连接中.....\r\n";
                                  tcpclient   =   new   tcpclient();
                                  tcpclient.connect("192.168.1.3",   9420);
                                  if   (tcpclient.connected)
                                  {
                                          ns   =   tcpclient.getstream();
                                          sr   =   new   streamreader(ns);
                                          sw   =   new   streamwriter(ns);
                                          iscon   =   true;
                                          this.label1.text   +=   "连接成功\r\n";
                                  }
                                  //iserror   =   true;
                          }
                          catch
                          {
                                  iserror=   true;
                                  this.label1.text   +=   "连接失败!\r\n";
                          }
                  }

          }  
发表于:2007-10-26 13:20:461楼 得分:0
有人能指點一下吗???
发表于:2007-10-26 13:47:102楼 得分:0
先锁定屏幕,然后得出屏幕图像以及选择框的面积与坐标,再取出相应面积的图片,垃圾回收。


快速检索

最新资讯
热门点击