捕获屏幕(5行快速方式)

大家好。

在本文中:您将知道以两种方式捕获屏幕:

1)-捕获全屏。

2)-捕获区域。

让我们开始:

首先创建一个新的Windows应用程序项目。

并确保您的程序使用以下名称空间:


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms; 
现在,我们将开始编写代码:

1)-创建一个新的位图对象。


//Bitmap screenBitmap = new Bitmap(int width, int height, PixelFormat);
Bitmap screenBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb); 
2)-创建一个新的矩形对象。

//screenRegion will grab the size of your current screen.
Rectangle screenRegion = Screen.AllScreens[0].Bounds; 
3)-创建一个新的图形对象。

Graphics screenGraphics = Graphics.FromImage(screenBitmap); 
4)-从屏幕上复制图像。

//screenGraphics will copy the image from the screen.
screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size); 
5)-保存图片:

screenBitmap.Save(@"C:\Screen.jpg", ImageFormat.Jpeg); 
这是代码:

Bitmap screenBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
Rectangle screenRegion = Screen.AllScreens[0].Bounds; 
// It will copy the current screep image to the bitmap image.
Graphics screenGraphics = Graphics.FromImage(screenBitmap);
screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);  
screenBitmap.Save(@"c:\test.jpg", ImageFormat.Jpeg); 

您还可以捕获屏幕的一部分(区域):


/* The Code was:
   screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size); 
*/ 
//See the difference. 
//screenGraphics.CopyFromScreen(intSourceX, intSourceY......);
screenGraphics.CopyFromScreen(200, 300, 0, 0, screenRegion.Size); 
希望这对您有所帮助。

From: https://bytes.com/topic/net/insights/666080-capture-screen-express-way-5-lines

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值