標籤:mod blog 視窗 改換 work 資訊 大小 建立 csdn
轉自http://blog.csdn.net/hailiannanhai/article/details/6281471
要想完成這個功能,首先要瞭解一下在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 ) ;//建立以螢幕大小為標準的位元影像
在C#中,可以使用System.Windows.SystemParameters擷取有關螢幕真實狀態的基本資料。
1. 擷取螢幕像素:
SystemParameters.FullPrimaryScreenHeight
SystemParameters.FullPrimaryScreenWidth
2. 擷取工作區(不包括工作列)像素:
SystemParameters.WorkArea.Height SystemParameters.WorkArea.Width
C# 獲得螢幕大小
Rectangle rect=System.Windows.Forms.SystemInformation.VirtualScreen; int width=rect.Width; int height=rect.Height;
(3).獲得當前螢幕和此Bitmap對象的DC,這可以通過下列語句實現:
//得到螢幕的DCIntPtr dc1 = g1.GetHdc ( ) ;//得到Bitmap的DCIntPtr dc2 = g2.GetHdc ( ) ;
(4).調用API函數,把當前螢幕拷貝到建立的Bitmap中:
BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;
(5).釋放當前螢幕和此Bitmap對象的DC,通過下面代碼可以實現:
//釋放掉螢幕的DCg1.ReleaseHdc ( dc1 ) ;//釋放掉Bitmap的DCg2.ReleaseHdc ( dc2 ) ;
(6).儲存Bitmap對象,形成jpg圖片:
MyImage.Save ( @"c:Capture.jpg" , ImageFormat.Jpeg );
當然你也可以根據自己的需要,把螢幕以其他圖片的格式來儲存,如果你想把圖片儲存為位元影像檔案,可以把"ImageFormat.Jpeg"改換成"ImageFormat.Bmp";想把圖片儲存為Gif檔案,就把"ImageFormat.Jpeg"改換成"ImageFormat.Gif"。你可以儲存的檔案類型大概有十多種,這裡就不一一介紹了,當然你也要相應改變儲存檔案的尾碼。
用C#來捕獲螢幕的來源程式代碼(Capture.cs):
瞭解上面的這些步驟的實現方法,就可以得到用C#捕獲螢幕的來源程式,如下:
using System ;using System.Drawing ;using System.Collections ;using System.ComponentModel ;using System.Windows.Forms ;using System.Data ;using System.Drawing.Imaging ;public class Form1 : Form.{private Button button1 ;private System.ComponentModel.Container components = null ;public Form1 ( ){//初始化表單中的各個組件InitializeComponent ( ) ;}// 清除程式中使用過的資源protected override void Dispose ( bool disposing ){if ( disposing ){if ( components != null ){components.Dispose ( ) ;}}base.Dispose ( disposing ) ;}private void InitializeComponent ( ){button1 = new Button ( );SuspendLayout ( ) ;button1.Location = new System.Drawing.Point ( 64 , 40 ) ;button1.Name = "button1" ;button1.Size = new System.Drawing.Size ( 80 , 32 ) ;button1.TabIndex = 0 ;button1.Text = "捕獲" ;button1.Click += new System.EventHandler ( button1_Click ) ;AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;ClientSize = new System.Drawing.Size ( 216 , 125 ) ;Controls.Add ( button1 ) ;MaximizeBox = false ;MinimizeBox = false ;Name = "Form1" ;Text = "C#捕獲當前螢幕!" ;ResumeLayout ( false ) ;}//聲明一個API函數[ 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 // 光柵的處理數值) ;static void Main ( ){Application.Run ( new Form1 ( ) ) ;}private void button1_Click ( object sender , System.EventArgs e ){//獲得當前螢幕的大小Rectangle rect = new Rectangle ( ) ;rect = Screen.GetWorkingArea ( this ) ;//建立一個以當前螢幕為模板的圖象Graphics g1 = this.CreateGraphics ( ) ;//建立以螢幕大小為標準的位元影像Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ;Graphics g2 = Graphics.FromImage ( MyImage ) ;//得到螢幕的DCIntPtr dc1 = g1.GetHdc ( ) ;//得到Bitmap的DCIntPtr dc2 = g2.GetHdc ( ) ;//調用此API函數,實現螢幕捕獲BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;//釋放掉螢幕的DCg1.ReleaseHdc ( dc1 ) ;//釋放掉Bitmap的DCg2.ReleaseHdc ( dc2 ) ;//以JPG檔案格式來儲存MyImage.Save ( @"c:Capture.jpg" , ImageFormat.Jpeg );MessageBox.Show ( "當前螢幕已經儲存為C盤的capture.jpg檔案!" ) ;}}
[轉]c#調用API