一個抓取電腦螢幕的小控制項台程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
namespace DeskRegistrar
{
class Program
{
//聲明一個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 // 光柵的處理數值
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateDC(
string lpszDriver, // 驅動名稱
string lpszDevice, // 裝置名稱
string lpszOutput, // 無用,可以設定位"NULL"
IntPtr lpInitData // 任意的印表機資料
);
static void Main(string[] args)
{
System.Threading.Timer timer = new System.Threading.Timer(delegate(Object o)
{
IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
//建立顯示器的DC
Graphics g1 = Graphics.FromHdc(dc1);
//由一個指定裝置的控制代碼建立一個新的Graphics對象
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);
//釋放位元影像控制代碼
MyImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg", ImageFormat.Jpeg);
//MessageBox.Show("已經把當前螢幕儲存到C:\\MyJpeg.jpg檔案中!");
}, null, 0, 3000);
Console.Read();
}
}
}