c#实现winform屏幕截图并保存的示例
using system.runtime.interopservices;
using system.drawing.imaging;
[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 // 光栅的处理数值
);
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 ) ;
//得到屏幕的dc
intptr dc1 = g1.gethdc ( ) ;
//得到bitmap的dc
intptr dc2 = g2.gethdc ( ) ;
//调用此api函数,实现屏幕捕获
bitblt ( dc2 , 0 , 0 , rect.width , rect.height , dc1 , 0 , 0 , 13369376 ) ;
//释放掉屏幕的dc
g1.releasehdc ( dc1 ) ;
//释放掉bitmap的dc
g2.releasehdc ( dc2 ) ;
//以jpg文件格式来保存
myimage.save ( @"c:/capture.jpg" , imageformat.jpeg );
messagebox.show ( "当前屏幕已经保存为c盘的capture.jpg文件!" ) ;
}