捕获当前窗体:
1 2 3 4 5 6 7
| 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); //保存图片
|
捕获整个屏幕:
1 2 3 4 5 6 7 8 9
| 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 窗体一起捕获进去了。解决办法:当点击捕获屏幕按钮时,将当前窗体隐藏,截图成功后再显示。
这是一篇过去很久的文章,其中的信息可能已经有所发展或是发生改变。