在C#案例開發這本書中提到了一個方法
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
namespace zhua2
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 表單設計器支援所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 表單設計器產生的程式碼
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 24);
this.button1.TabIndex = 1;
this.button1.Text = "開始抓圖";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// saveFileDialog1
//
this.saveFileDialog1.FileName = "doc1";
this.saveFileDialog1.Filter = "jpg Files(*.jpg) ¦*.jpg ¦jpeg Files(*.*) ¦*.jpeg ¦bmp Files(*.bmp) ¦*.bmp";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 93);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "抓圖軟體";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//調用動態連結程式庫gdi32.dll
[ 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 // 光柵的操作值
) ;
//調用動態連結程式庫gdi32.dll
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern IntPtr CreateDC (
string lpszDriver , // 驅動名稱
string lpszDevice , // 裝置名稱
string lpszOutput , // 無用,可以設定位"NULL"
IntPtr lpInitData // 任意的印表機資料
) ;
private void button1_Click(object sender, System.EventArgs e)
{
this.Hide();
IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;
//建立顯示器的DC
Graphics g1 = Graphics.FromHdc ( dc1 ) ;
//由一個指定裝置的控制代碼建立一個新的Graphics對象
Bitmap 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 ) ;
//釋放位元影像控制代碼
if (saveFileDialog1.ShowDialog () == DialogResult.OK )
{
MyImage.Save ( saveFileDialog1.FileName, ImageFormat.Bmp ) ;
MessageBox.Show ( "已經把當前螢幕儲存!" ) ;
this.Show();
}
}
}
}