抓取屏幕,分析屏幕上的目标小图片位置,代码犀利,速度很快
2015-07-17 15:22
381 查看
原文:http://www.codeproject.com/Articles/25025/Screen-Scraper-in-Managed-Code
核心代码:
获取屏幕图片
demo:
桌面
![](http://img.blog.csdn.net/20150717151524858?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
我从右侧的广告上截了个小图:
![](http://img.blog.csdn.net/20150717151608528?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
用工具查找下:
![](http://img.blog.csdn.net/20150717151853219?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
很是牛B吧,作者本来打算用来分析网页源码的,这么搞也是很有想法。
几点注意的地方:
There are a couple of things to keep in mind when using this program:
Requires the images loaded are in 32 bit ARGB format
The time it takes to run is dependent on the existence of a good unique pixel value
On a 2 GHz Athelon with a 15.4'' screen, about .5 sec. for most images
About 30 sec. for small white images and white screen background
The Print Screen functionality was only tested on Windows XP, and may not work the same on Vista, etc.
Screen Scraper in Managed Code
核心代码:public List<Point> findImages() { Bitmap bm = getDesktopBitmap(); BitmapData bmd = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, bm.PixelFormat); List<Point> results = new List<Point>(); foundRects = new List<Rectangle>(); for (int y = 0; y < bmd.Height; y++) { byte* scanline = (byte*)bmd.Scan0 + (y * bmd.Stride); for (int x = 0; x < bmd.Width; x++) { int xo = x * PIXLESIZE; byte[] buff = { scanline[xo], scanline[xo + 1], scanline[xo + 2], 0xff }; int val = BitConverter.ToInt32(buff, 0); // Pixle value from subimage in desktop image if (pixels.ContainsKey(val) && notFound(x, y)) { Point loc = (Point)pixels[val]; int sx = x - loc.X; int sy = y - loc.Y; // Subimage occurs in desktop image if (imageThere(bmd, sx, sy)) { Point p = new Point(x - loc.X, y - loc.Y); results.Add(p); foundRects.Add(new Rectangle(x, y, bmImage.Width, bmImage.Height)); } } } } return results; } private bool imageThere(BitmapData bmd, int sx, int sy) { int ix; for (int iy = 0; iy < bmImage.Height; iy++) { // Horizontal line of pixles in the bitmap data byte* scanline = (byte*)bmd.Scan0 + ((sy + iy) * bmd.Stride); for (ix = 0; ix < bmImage.Width; ix++) { // Offset into the scan line int xo = (sx + ix) * PIXLESIZE; // Convert PixelFormat.Format24bppRgb // to PixelFormat.Format32bppArgb byte[] buff = { scanline[xo], scanline[xo + 1], scanline[xo + 2], 0xff }; // Pixle value int val = BitConverter.ToInt32(buff, 0); if (val != image[ix, iy]) return false; } ix = 0; } return true; } private bool notFound(int x, int y) { Point p = new Point(x, y); foreach (Rectangle r in foundRects) { if (r.Contains(p)) return false; } return true; }
获取屏幕图片
private static Bitmap getDesktopBitmap() { SendKeys.SendWait("^{PRTSC}"); Bitmap bm = new Bitmap(Clipboard.GetImage()); Clipboard.Clear(); return bm; }
demo:
桌面
我从右侧的广告上截了个小图:
用工具查找下:
很是牛B吧,作者本来打算用来分析网页源码的,这么搞也是很有想法。
几点注意的地方:
There are a couple of things to keep in mind when using this program:
Requires the images loaded are in 32 bit ARGB format
The time it takes to run is dependent on the existence of a good unique pixel value
On a 2 GHz Athelon with a 15.4'' screen, about .5 sec. for most images
About 30 sec. for small white images and white screen background
The Print Screen functionality was only tested on Windows XP, and may not work the same on Vista, etc.
相关文章推荐
- 问题-[DelphiXE2]编译程序体积大的问题
- JAVA NIO异步通信框架MINA选型和使用的几个细节(概述入门,UDP, 心跳)
- lua面向对象编程中冒号与点号的区别
- 问题-[delphi2007、2010]无法二次启动,报EditorLineEnds.ttr被占用,进程一直有bds.exe?
- PHP导出一个txt文本文件
- 聊聊高并发(四十一)解析java.util.concurrent各个组件(十七) 任务的异步执行和状态控制
- 问题-[Delphi]通过Map文件查找内存地址出错代码所在行
- c++ 如何输出大数不用科学计数法 和如何保留几位小数输出
- gradle构建代码加速(即AS更快打开其他项目)的方法
- PHP 多文件上传 $_FILES 为空
- 问题-[DelphiXE7]新建的安桌模拟器运行程序闪退
- Qt数据库:(二)添加MySQL驱动插件
- 问题-[Delphi]用LoadLibrary加载DLL时返回0的错误
- java数学归纳法非递归求斐波那契数列的方法
- Spark编程模型
- JAVA 继承与组合
- Spark编程模型
- C# set get
- java中List的排序功能的实现
- Qt-优化布局结构