zoukankan      html  css  js  c++  java
  • C#学习笔记——捕获当前屏幕

    编程思路(API 编程):
    先调用 GetForegroundWindow 获取当前活动程序窗口句柄,然后调用 GetWindowDC 获取窗口的设备句柄(或 GetDC 函数),调用 BitBlt 位图传输函数将位图拷贝到兼容的设备场景中(拷贝时可以指定位置和大小),最后保存位图文件。

    以下源代码内容转自 CSDN 论坛。

    要想完成这个功能,首先要了解一下在C#中如何调用API(应用程序接口)函数。虽然在.Net框架中已经提供了许多类库,这些类库的功能也十分强大,但对于一些Windows底层编程来说,还是要通过调用这些API函数才可以实现。所有API都在"Kernel"、"User "和"GDI"三个库中得以运行:其中"Kernel",他的库名为 "KERNEL32.DLL", 他主要用于产生与操作系统之间的关联,譬如:程序加载,上下文选择,文件输入输出,内存管理等等。"User "这个类库在Win32中名叫 "USER32.DLL"。 它允许管理全部的用户接口。譬如:窗口 、菜单 、对话框 、图标等等。"GDI"(图象设备接口),它在Win32中的库名为:"GDI32.dll",它是图形输出库。使用GDI Windows"画"出窗口、菜单以及对话框等;它能创建图形输出;它也能保存图形文件。由于本文所涉及到是图象问题,所有调用的类库是"GDI32.dll"。在本文程序中我们使用的API函数是"BitBlt",这个函数对于广大程序员来说,一定不感觉到陌生,因为在图象处理方面他的用途是相对广的,在用其他程序语言编程中,时常也要和他打交道。在.Net FrameWork SDK中有一个名字空间"System.Runtime.InteropServices",此名字空间提供了一系列的类来访问COM对象,和调用本地的API函数。下面是在C#中声明此函数:

    [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ] 
    private static extern bool BitBlt ( 
    IntPtr hdcDest , // 目标 DC的句柄 
    int nXDest , 
    int nYDest , 
    int nWidth , 
    int nHeight , 
    IntPtr hdcSrc , // 源DC的句柄 
    int nXSrc , 
    int nYSrc , 
    System.Int32 dwRop // 光栅的处理数值 
    ) ;

    通过上面这个声明,就可以在下面的代码中使用此函数了。

    下面是用C#做屏幕捕获程序的具体实现步骤:

    (1).首先要获得当前屏幕的graphic对象,通过以下代码可以实现:

    Graphics g1 = this.CreateGraphics ( ) ;

    (2).创建一个Bitmap对象,并且这个Bitmap对象的大小是当前屏幕:

    首先要获得当前屏幕的大小,通过名字空间"System.Windows.Forms"中的"Screen"类的GetWorkingArea()方法,可以实现。下面是得到当前屏幕的长(Height)和宽(Width):

    Rectangle rect = new Rectangle ( ) ; 
    rect = Screen.GetWorkingArea ( this ) ; 
    "屏幕宽" = rect.Width ; 
    "屏幕长" = rect.Height ;

    至此就可以得到我们想要的Bitmap了,通过下列语句可以实现:

    Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ; 
    //创建以屏幕大小为标准的位图

    (3).获得当前屏幕和此Bitmap对象的DC,这可以通过下列语句实现:

    //得到屏幕的DC 
    IntPtr dc1 = g1.GetHdc ( ) ; 
    //得到Bitmap的DC 
    IntPtr dc2 = g2.GetHdc ( ) ;

    (4).调用API函数,把当前屏幕拷贝到创建的Bitmap中:

    BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;

    (5).释放当前屏幕和此Bitmap对象的DC,通过下面代码可以实现:

    //释放掉屏幕的DC 
    g1.ReleaseHdc ( dc1 ) ; 
    //释放掉Bitmap的DC 
    g2.ReleaseHdc ( dc2 ) ;

    (6).保存Bitmap对象,形成jpg图片:

    MyImage.Save ( @"c:\Capture.jpg" , ImageFormat.Jpeg );

    当然你也可以根据自己的需要,把屏幕以其他图片的格式来保存,如果你想把图片保存为位图文件,可以把"ImageFormat.Jpeg"改换成"ImageFormat.Bmp";想把图片保存为Gif文件,就把"ImageFormat.Jpeg"改换成"ImageFormat.Gif"。你可以保存的文件类型大概有十多种,这里就不一一介绍了,当然你也要相应改变保存文件的后缀。

    用C#来捕获屏幕的源程序代码(Capture.cs):

       1: using System ; 
       2: using System.Drawing ; 
       3: using System.Collections ; 
       4: using System.ComponentModel ; 
       5: using System.Windows.Forms ; 
       6: using System.Data ; 
       7: using System.Drawing.Imaging ; 
       8: public class Form1 : Form 
       9: { 
      10:     private Button button1 ; 
      11:     private System.ComponentModel.Container components = null ; 
      12:  
      13:     public Form1 ( ) 
      14:     { 
      15:         //初始化窗体中的各个组件 
      16:         InitializeComponent ( ) ; 
      17:     } 
      18:     // 清除程序中使用过的资源 
      19:     protected override void Dispose ( bool disposing ) 
      20:     { 
      21:         if ( disposing ) 
      22:         { 
      23:             if ( components != null ) 
      24:             { 
      25:                 components.Dispose ( ) ; 
      26:             } 
      27:         } 
      28:         base.Dispose ( disposing ) ; 
      29:     } 
      30:     private void InitializeComponent ( ) 
      31:     { 
      32:         button1 = new Button ( ); 
      33:         SuspendLayout ( ) ; 
      34:         button1.Location = new System.Drawing.Point ( 64 , 40 ) ; 
      35:         button1.Name = "button1" ; 
      36:         button1.Size = new System.Drawing.Size ( 80 , 32 ) ; 
      37:         button1.TabIndex = 0 ; 
      38:         button1.Text = "捕获" ; 
      39:         button1.Click += new System.EventHandler ( button1_Click ) ; 
      40:  
      41:         AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ; 
      42:         ClientSize = new System.Drawing.Size ( 216 , 125 ) ; 
      43:         Controls.Add ( button1 ) ; 
      44:         MaximizeBox = false ; 
      45:         MinimizeBox = false ; 
      46:         Name = "Form1" ; 
      47:         Text = "C#捕获当前屏幕!" ; 
      48:         ResumeLayout ( false ) ; 
      49:  
      50:     } 
      51:     //声明一个API函数 
      52:     [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ] 
      53:     private static extern bool BitBlt ( 
      54:         IntPtr hdcDest , // 目标 DC的句柄 
      55:         int nXDest , 
      56:         int nYDest , 
      57:         int nWidth , 
      58:         int nHeight , 
      59:         IntPtr hdcSrc , // 源DC的句柄 
      60:         int nXSrc , 
      61:         int nYSrc , 
      62:         System.Int32 dwRop // 光栅的处理数值 
      63:         ) ; 
      64:  
      65:     static void Main ( ) 
      66:     { 
      67:         Application.Run ( new Form1 ( ) ) ; 
      68:     } 
      69:     private void button1_Click ( object sender , System.EventArgs e ) 
      70:     { 
      71:         //获得当前屏幕的大小 
      72:         Rectangle rect = new Rectangle ( ) ; 
      73:         rect = Screen.GetWorkingArea ( this ) ; 
      74:         //创建一个以当前屏幕为模板的图象 
      75:         Graphics g1 = this.CreateGraphics ( ) ; 
      76:         //创建以屏幕大小为标准的位图 
      77:         Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ; 
      78:         Graphics g2 = Graphics.FromImage ( MyImage ) ; 
      79:         //得到屏幕的DC 
      80:         IntPtr dc1 = g1.GetHdc ( ) ; 
      81:         //得到Bitmap的DC 
      82:         IntPtr dc2 = g2.GetHdc ( ) ; 
      83:         //调用此API函数,实现屏幕捕获 
      84:         BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ; 
      85:         //释放掉屏幕的DC 
      86:         g1.ReleaseHdc ( dc1 ) ; 
      87:         //释放掉Bitmap的DC 
      88:         g2.ReleaseHdc ( dc2 ) ; 
      89:         //以JPG文件格式来保存 
      90:         MyImage.Save ( @"c:\Capture.jpg" , ImageFormat.Jpeg ); 
      91:         MessageBox.Show ( "当前屏幕已经保存为C盘的capture.jpg文件!" ) ; 
      92:     } 
      93: }

    原文来自:http://xugang.cnblogs.com

  • 相关阅读:
    CPU密集型、IO密集型
    打印在一行
    python多线程、线程锁
    WPS--world使用格式刷
    pycharm连接linux版python
    Pycharm连接windows上python
    一台windows主机上运行2个tomcat
    启动django应用报错 “Error: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试。”
    ORA-03206,当表空间不够时,如何以添加数据文件的方式扩展表空间
    利用拷贝data目录文件的方式迁移mysql数据库
  • 原文地址:https://www.cnblogs.com/hanzhaoxin/p/2909589.html
Copyright © 2011-2022 走看看