using system.data ;
using system.drawing.imaging ;
using system.io ;
//导入在程序中使用到的名称空间
public class capture : form
{
private system.componentmodel.container components = null ;
private icon mnettrayicon = new icon ( "tray.ico" ) ;
private bitmap myimage = null ;
private notifyicon trayicon ;
private contextmenu notifyiconmnu ;
public capture ( )
{
//初始化窗体中使用到的组件
initializecomponent ( ) ;
}
protected override void onactivated ( eventargs e )
{
this.hide ( ) ;
}
[ 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 // 光栅的操作值
) ;
[ system.runtime.interopservices.dllimportattribute ( "gdi32.dll" ) ]
private static extern intptr createdc (
string lpszdriver , // 驱动名称
string lpszdevice , // 设备名称
string lpszoutput , // 无用,可以设定位"null"
intptr lpinitdata // 任意的打印机数据
) ;
public void capture ( object sender , system.eventargs e )
{
this.visible = false ;
intptr dc1 = createdc ( "display" , null , null , ( intptr ) null ) ;
//创建显示器的dc
graphics g1 = graphics.fromhdc ( dc1 ) ;
//由一个指定设备的句柄创建一个新的graphics对象
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 ( "c:\\myjpeg.jpg" , imageformat.jpeg ) ;
messagebox.show ( "已经把当前屏幕保存到c:\\myjpeg.jpg文件中!" ) ;
this.visible = true ;
}
public void exitselect ( object sender , system.eventargs e )
{
//隐藏托盘程序中的图标
trayicon.visible = false ;
//关闭系统
this.close ( ) ;
}
//清除程序中使用过的资源
/*public override void dispose( )
{
base.dispose ( ) ;
if ( components != null )
components.dispose ( ) ;
} */
private void initializecomponent ( )
{
//设定托盘程序的各个属性
trayicon = new notifyicon ( ) ;
trayicon.icon = mnettrayicon ;
trayicon.text = "用c#做screen capture程序" ;
trayicon.visible = true ;
//定义一个menuitem数组,并把此数组同时赋值给contextmenu对象
menuitem [ ] mnuitms = new menuitem [ 3 ] ;
mnuitms [ 0 ] = new menuitem ( ) ;
mnuitms [ 0 ] .text = "捕获当前屏幕!" ;
mnuitms [ 0 ] .click += new system.eventhandler ( this.capture ) ;
mnuitms [ 1 ] = new menuitem ( "-" ) ;
mnuitms [ 2 ] = new menuitem ( ) ;
mnuitms [ 2 ] .text = "退出系统" ;
mnuitms [ 2 ] .click += new system.eventhandler ( this.exitselect ) ;
mnuitms [ 2 ] .defaultitem = true ;
notifyiconmnu = new contextmenu ( mnuitms ) ;
trayicon.contextmenu = notifyiconmnu ;
//为托盘程序加入设定好的contextmenu对象
this.suspendlayout ( ) ;
this.autoscalebasesize = new system.drawing.size ( 5 , 13 ) ;
this.clientsize = new system.drawing.size ( 320 , 56 ) ;
this.controlbox = false ;
this.maximizebox = false ;
this.minimizebox = false ;
this.windowstate = system.windows.forms.formwindowstate.minimized ;
this.name = "capture" ;
this.showintaskbar = false ;
this.text = "用c#做screen capture程序!" ;
this.resumelayout ( false ) ;
}
static void main ( )
{
application.run ( new capture ( ) ) ;
}
}