C# 捕获屏幕源码

2016-10-31
次阅读
1 分钟阅读时长

捕获当前窗体:

string Path = @"E:/Test/";  //文件保存路径
int width = this.Size.Width;    //当前窗体宽度
int heigh = this.Size.Height;   //当前窗体高度
Bitmap bmp = new Bitmap(width, heigh);  //新建一个 Bitmap 位图 
string FileName = Path + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + new Random().Next(999) + ".jpg";
this.DrawToBitmap(bmp, new Rectangle(0, 0, width, heigh));  //将当前屏幕画到 Bitmap 位图上
bmp.Save(FileName, System.Drawing.Imaging.ImageFormat.Jpeg);    //保存图片

捕获整个屏幕:

string Path = @"E:/Test/";  //文件保存路径
int width = Screen.PrimaryScreen.Bounds.Width;  //主屏幕宽度
int height = Screen.PrimaryScreen.Bounds.Height;    //主屏幕高度
Bitmap bmp = new Bitmap(width, height);     //新建一个 Bitmap 位图 
Graphics g = Graphics.FromImage(bmp);   //从 Bitmap 位图创建 Graphics
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(width, height));
g.ReleaseHdc(g.GetHdc());
string FileName = Path + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + new Random().Next(999) + ".jpg";
bmp.Save(FileName);

源码下载:x

效果图:x

不足:捕获时将 Winform 窗体一起捕获进去了。解决办法:当点击捕获屏幕按钮时,将当前窗体隐藏,截图成功后再显示。

这是一篇过去很久的文章,其中的信息可能已经有所发展或是发生改变。

本文作者:她和她的猫
本文地址https://her-cat.com/posts/2016/10/31/c-sharp-capture-screen/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!