在掌握了一些C#源代码后,可以得到用C#做Screen Capture程序的源代码(Capture.cs),具体如下:
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.componentmodel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Drawing.Imaging;
usingSystem.IO;
//导入在程序中使用到的名称空间
publicclassCapture:Form
{
privateSystem.ComponentModel.Containercomponents=null;
privateIconmNetTrayIcon=newIcon("Tray.ico");
privateBitmapMyImage=null;
privatenotifyiconTrayIcon;
privateContextMenunotifyiconMnu;
publicCapture()
{
//初始化窗体中使用到的组件
}
protectedoverridevoidOnActivated(EventArgse)
{
this.Hide();
}
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
privatestaticexternboolBitBlt(
IntPtrhdcDest,//目标设备的句柄
intnXDest,//目标对象的左上角的X坐标
intnYDest,//目标对象的左上角的X坐标
intnWidth,//目标对象的矩形的宽度
intnHeight,//目标对象的矩形的长度
IntPtrhdcSrc,//源设备的句柄
intnXSrc,//源对象的左上角的X坐标
intnYSrc,//源对象的左上角的X坐标
System.Int32dwrop//光栅的操作值
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
privatestaticexternIntPtrcreatedC(
stringlpszDriver,//驱动名称
stringlpszDevice,//设备名称
stringlpszOutput,//无用,可以设定位"NULL"
IntPtrlpInitData//任意的打印机数据
);
publicvoidcapture(objectsender,System.EventArgse)
{
this.Visible=false;
IntPtrdc1=CreateDC("DISPLAY",null,null,(IntPtr)null);
//创建显示器的DC
Graphicsg1=Graphics.FromHdc(dc1);
//由一个指定设备的句柄创建一个新的Graphics对象
MyImage=newBitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,g1);//根据屏幕大小创建一个与之相同大小的Bitmap对象
Graphicsg2=Graphics.FromImage(MyImage);
//获得屏幕的句柄
IntPtrdc3=g1.GetHdc();
//获得位图的句柄
IntPtrdc2=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;
}
publicvoidExitSelect(objectsender,System.EventArgse)
{
//隐藏托盘程序中的图标
TrayIcon.Visible=false;
//关闭系统
this.Close();
}
//清除程序中使用过的资源
publicoverridevoidDispose()
{
base.Dispose();
if(components!=null)
components.Dispose();
}
privatevoidInitializeComponent()
{
//设定托盘程序的各个属性
TrayIcon=newNotifyIcon();
TrayIcon.Icon=mNetTrayIcon;
TrayIcon.Text="用C#做ScreenCapture程序";
TrayIcon.Visible=true;
//定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象
MenuItem[]mnuItms=newMenuItem[3];
mnuItms[0]=newMenuItem();
mnuItms[0].Text="捕获当前屏幕!";
mnuItms[0].Click+=newSystem.EventHandler(this.capture);
mnuItms[1]=newMenuItem("-");
mnuItms[2]=newMenuItem();
mnuItms[2].Text="退出系统";
mnuItms[2].Click+=newSystem.EventHandler(this.ExitSelect);
mnuItms[2].DefaultItem=true;
notifyiconMnu=newContextMenu(mnuItms);
TrayIcon.ContextMenu=notifyiconMnu;
//为托盘程序加入设定好的ContextMenu对象
this.SuspendLayout();
this.AutoScaleBaseSize=newSystem.Drawing.Size(5,13);
this.ClientSize=newSystem.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#做ScreenCapture程序!";
this.ResumeLayout(false);
}
staticvoidMain()
{
Application.Run(newCapture());
}
}
感谢各位的阅读,以上就是“怎么用C#做Screen Capture程序”的内容了,经过本文的学习后,相信大家对怎么用C#做Screen Capture程序这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是本站,小编将为大家推送更多相关知识点的文章,欢迎关注!
本文由 贵州做网站公司 整理发布,部分图文来源于网络,如有侵权,请联系我们删除,谢谢!
抖音(Tiktok)是由北京字节跳动科技有限公司(ByteDance)孵化的音乐创意短视频社交网站。它于2016年9月20日上线,主要面向全年龄段的用户,尤其是...
飞极速为您提供高清在线电影、电视剧大全、动画片,综艺等,飞极速在线更新及时,播放速度快,给您带来最好的观影体验!飞极速在线,免费提供日本新番动漫、最新电影和最新...
《南华早报》(英语:South China Morning Post, SCMP)和星期日出版的《星期日南华早报》(英语:Sunday Morning Post...
据2022年三大运营公布的最新数据显示,中国移动用户数量排名第一。据中国移动,2022年1月公司及其各附属公司移动业务净增客户数449.7万户,客户总数达9.61389亿户,其中5G套餐客户数4.0127亿户。去年底,中国移动的5G套餐客户数约3.87亿户。据中国电信,2022年1月移动用户数净增307万户,总数达3.7550亿户;其中,5G套餐用户数净增826万户,总数达1.9606亿户。中国联...
支付宝亲密付额度是多少?据了解,现在亲密付每月额度为100元到2万元人民币不等,而且亲密付的额度是由亲密付开通人设置的。额度关系也是一对一的关系,若开通人给多个账户开通了亲密付,那么彼此的额度可以分开管理。因此,有需求的朋友在支付宝首页直接搜索“亲情卡”,或在支付宝“首页”点击功能栏的“全部”,再在资金往来一栏中点击&ldqu...
什么是库克曲线?美国学者库克(Kuck)提出了另外一条曲线,从如何更好地发挥人的创造力的角度论证了员工流动的必要性,如下图所示库克曲线是根据研究生参加工作后创造力发挥情况所作的统计绘出曲线的。库克曲线什么时候提出的?库克曲线,也叫Kocker-Le Chatelier曲线,是工程师埃利·库克提出的一种特殊的热力学状态图。它描述了等温外墙绝热容积与物质物性之间的关系,在物质物性变化时,...