//屏幕抓图——C#实现抓屏功能“gdi32.dll”
系统:xp
编程环境:Microsoft Visual Studio 2010
语言:C#//代码如下
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace 屏幕抓图
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[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 //光栅的处理数值
);
private void button1_Click(object sender, EventArgs e)
{
Rectangle rect = Screen.GetWorkingArea(this); //获得当前屏幕的大小
Graphics g1 = this.CreateGraphics(); //获得当前窗体画布对象
Image MyImage = new Bitmap(rect.Width, rect.Height, g1);//创建以屏幕大小为标准的位图
Graphics g2 = Graphics.FromImage(MyImage); //使用位图实力创建画布对象
IntPtr dc1 = g1.GetHdc(); //得到窗体画布的 DC
IntPtr dc2 = g2.GetHdc(); //得到 Bitmap 的 DC
BitBlt(dc2, 0, 0, rect.Width, rect.Height, dc1, 0 - this.Location.X, 0 - this.Location.Y, 13369376);
g1.ReleaseHdc(dc1); //释放屏幕的 DC
g2.ReleaseHdc(dc2); //释放 Bitmap 的 DC
MyImage.Save(@"c:\1.jpg", ImageFormat.Jpeg); //保存抓屏图片
MessageBox.Show("当前屏幕已经保存为C盘的1.jpg文件!");
}
}
}