发布时间:2025-12-11 01:28:15 浏览次数:1
在掌握了一些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()
{
//初始化窗体中使用到的组件
InitializeComponent();
}
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程序这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是本站,小编将为大家推送更多相关知识点的文章,欢迎关注!