[翻译]C#基础演练:屏幕抓取
2011-07-18 15:38
441 查看
TeboScreen:BasicC#ScreenCaptureApplication
源英文文档<http://www.codeproject.com/KB/cs/TeboScreen.aspx>
翻译:白水引用请注明出处<敏捷学院>
源代码下载:http://dev.mjxy.cn/a-CSharp-based-exercises-screen-capture.aspx
Introduction
应用程序抓取屏幕有两种不同的方式:
抓取屏幕:抓取整个屏幕,然后将图像保存到指定的图片文件中。
抓取区域:用户按住鼠标左键,绘制一个矩形,指定他们希望捕捉屏幕的一部分。在释放鼠标左键,然后指定一个文件名将扑捉到的矩形图像保存到指定的文件中。
背景
这个应用程序是我一个C#学习的练习。我想看看使用C#如何简单的创建一个屏幕保存程序,最后我花了一个下午的时间写应用程序。
我决定使用一个简单的方式来解决如何让用户在屏幕上画一个矩形。最大化应用程序窗口,并使它透明度为30%。先绘制一个矩形,然后使用窗体背景色绘制一个矩形用来擦出以前的矩形。由于窗体是最大化的,任何坐标位置正好对应屏幕上的坐标。ScreenShot.CaptureImage方法绘制矩形并返回矩形的坐标。
代码分析
两个模块实现屏幕抓取
CaptureScreen
我们需要做的就是将屏幕的区域传递给ScreenShot.CaptureImage方法。这里唯一需要注意的地方就是我们暂停了250毫秒,让屏幕重绘。如果不这样做,直接执行下一行代码,我们应用程序的本身也会被扑捉到。
捕抓一个区域
用户按住鼠标左键,绘制一个矩形,指定他们希望捕捉屏幕的一部分。在释放鼠标左键,然后指定一个文件名将扑捉到的矩形图像保存到指定的文件中。mouse_Move事件用来擦除之前绘制的矩形。
这里是调用ScreenShot.CaptureImage如何捕抓一个区域。
捕抓屏幕的代码定义在类ScreenShot中,包含一个静态的方法CaptureImage。
源英文文档<
翻译:白水引用请注明出处<
源代码下载:
Introduction
应用程序抓取屏幕有两种不同的方式:
抓取屏幕:抓取整个屏幕,然后将图像保存到指定的图片文件中。
抓取区域:用户按住鼠标左键,绘制一个矩形,指定他们希望捕捉屏幕的一部分。在释放鼠标左键,然后指定一个文件名将扑捉到的矩形图像保存到指定的文件中。
背景
这个应用程序是我一个C#学习的练习。我想看看使用C#如何简单的创建一个屏幕保存程序,最后我花了一个下午的时间写应用程序。
我决定使用一个简单的方式来解决如何让用户在屏幕上画一个矩形。最大化应用程序窗口,并使它透明度为30%。先绘制一个矩形,然后使用窗体背景色绘制一个矩形用来擦出以前的矩形。由于窗体是最大化的,任何坐标位置正好对应屏幕上的坐标。ScreenShot.CaptureImage方法绘制矩形并返回矩形的坐标。
代码分析
两个模块实现屏幕抓取
CaptureScreen
我们需要做的就是将屏幕的区域传递给ScreenShot.CaptureImage方法。这里唯一需要注意的地方就是我们暂停了250毫秒,让屏幕重绘。如果不这样做,直接执行下一行代码,我们应用程序的本身也会被扑捉到。
1 | //Allow250millisecondsforthescreentorepaintitself |
2 |
3 | //(wedon'twanttoincludethisforminthecapture) |
4 |
5 | System.Threading.Thread.Sleep(250); |
6 |
7 | Rectanglebounds=Screen.GetBounds(Screen.GetBounds(Point.Empty)); |
8 |
9 | ScreenShot.CaptureImage(Point.Empty,Point.Empty,bounds,ScreenPath); |
用户按住鼠标左键,绘制一个矩形,指定他们希望捕捉屏幕的一部分。在释放鼠标左键,然后指定一个文件名将扑捉到的矩形图像保存到指定的文件中。mouse_Move事件用来擦除之前绘制的矩形。
01 | private void mouse_Move( object sender,MouseEventArgse) |
02 |
03 | { |
04 |
05 | //Resize(actuallydeletethenre-draw)therectangleiftheleft |
06 |
07 | mousebutton is helddown |
08 |
09 | if (LeftButtonDown) |
10 |
11 | { |
12 |
13 | //Erasethepreviousrectangle |
14 |
15 | g.DrawRectangle(EraserPen,CurrentTopLeft.X,CurrentTopLeft.Y,CurrentBottomRight.X-CurrentTopLeft.X,CurrentBottomRight.Y- |
16 |
17 | CurrentTopLeft.Y); |
18 |
19 | //CalculateXCoordinates |
20 |
21 | if (Cursor.Position.X<ClickPoint.X) |
22 |
23 | { |
24 |
25 | CurrentTopLeft.X=Cursor.Position.X; |
26 |
27 | CurrentBottomRight.X=ClickPoint.X; |
28 |
29 | } |
30 |
31 | else |
32 |
33 | { |
34 |
35 | CurrentTopLeft.X=ClickPoint.X; |
36 |
37 | CurrentBottomRight.X=Cursor.Position.X; |
38 |
39 | } |
40 |
41 | //CalculateYCoordinates |
42 |
43 | if (Cursor.Position.Y<ClickPoint.Y) |
44 |
45 | { |
46 |
47 | CurrentTopLeft.Y=Cursor.Position.Y; |
48 |
49 | CurrentBottomRight.Y=ClickPoint.Y; |
50 |
51 | } |
52 |
53 | else |
54 |
55 | { |
56 |
57 | CurrentTopLeft.Y=ClickPoint.Y; |
58 |
59 | CurrentBottomRight.Y=Cursor.Position.Y; |
60 |
61 | } |
62 |
63 | //Drawanewrectangle |
64 |
65 | g.DrawRectangle(MyPen,CurrentTopLeft.X,CurrentTopLeft.Y,CurrentBottomRight.X-CurrentTopLeft.X,CurrentBottomRight.Y-CurrentTopLeft.Y); |
66 |
67 | } |
68 |
69 | } |
1 | PointStartPoint= new Point(ClickPoint.X,ClickPoint.Y); |
2 | Rectanglebounds= new Rectangle(ClickPoint.X,ClickPoint.Y,CurrentPoint.X-ClickPoint.X,CurrentPoint.Y-ClickPoint.Y); |
3 | ScreenShot.CaptureImage(StartPoint,Point.Empty,bounds,ScreenPath); |
01 | class ScreenShot |
02 |
03 | { |
04 |
05 | public static void CaptureImage(PointSourcePoint,PointDestinationPoint, |
06 |
07 | RectangleSelectionRectangle, string FilePath) |
08 |
09 | { |
10 |
11 | using (Bitmapbitmap= new Bitmap(SelectionRectangle.Width, |
12 |
13 | SelectionRectangle.Height)) |
14 |
15 | { |
16 |
17 | using (Graphicsg=Graphics.FromImage(bitmap)) |
18 |
19 | { |
20 |
21 | g.CopyFromScreen(SourcePoint,DestinationPoint, |
22 |
23 | SelectionRectangle.Size); |
24 |
25 | } |
26 |
27 | bitmap.Save(FilePath,ImageFormat.Bmp); |
28 |
29 | } |
30 |
31 | } |
32 |
33 | } |
相关文章推荐
- C#基础演练:屏幕抓取小工具
- C#抓取当前屏幕并保存为图片的方法
- 【C#】基础实例演练
- [原创]C#基础演练代码(3)
- C#语言实现对网页图片的随机抓取。基础办法,但还有一点小问题,欢迎大家指正
- 屏幕抓取(screen scraping ) c#版本 相关资料
- c#写的windows服务,定时抓取客户机的屏幕,并发送到指定的邮箱
- 屏幕抓取(screen scraping ) c#版本 相关资料
- C# 抓取屏幕图片
- (翻译) 《C# to IL》第二章 IL基础
- C#利用Windows自带gdi32.dll实现抓取屏幕功能实例
- c#写的windows服务,定时抓取客户机的屏幕,并发送到指定的邮箱
- C#远程控软件(1) 抓取屏幕 压缩 转换字节
- 015天(.net学习之路-C#基础知识)-正则表达式(UBB翻译、敏感词处理)
- C#利用Windows自带gdi32.dll实现抓取屏幕功能实例
- [原创]C#基础演练代码(1),持续更新中.......
- [原创]C#基础演练代码(2),持续更新中.......
- (翻译) 《C# to IL》第二章 IL基础
- C#基础笔记——语言基础
- C#基础---浅谈XML读取以及简单的ORM实现