抓取屏幕,分析屏幕上的目标小图片位置,代码犀利,速度很快
来源:互联网 发布:南阳理工软件 知乎 编辑:程序博客网 时间:2024/05/30 23:31
原文:http://www.codeproject.com/Articles/25025/Screen-Scraper-in-Managed-Code
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.
0 0
- 抓取屏幕,分析屏幕上的目标小图片位置,代码犀利,速度很快
- 获取屏幕位置、高度和在屏幕上的位置
- 得到光标在屏幕上的位置
- android view 屏幕的点击位置分析
- 捕获鼠标位置[在屏幕上的位置]
- vs下抓取屏幕选定部分的图片 并保存
- 抓取屏幕图像及光标的程序分析
- 利用VelocityTracker获取手指在屏幕上的滑动速度
- 计算机屏幕上的代码列
- 屏幕抓取的简单实现
- 笔记10:时时屏幕抓取小程序
- OSG中抓取屏幕保存为图片
- OSG中抓取屏幕保存为图片
- Java抓取屏幕保存为图片
- C++屏幕抓图代码- 抓取屏幕 保存位图
- 获取组件在屏幕上的坐标位置
- javascript:如何获得鼠标在屏幕上的位置
- C# 获取光标在屏幕上的位置
- 150mbps MINI WIRELESS N USB ADAPTER always broken ,you can install driver like this in ubuntu
- Realm 学习(一)——平台搭建
- C# set get
- min3d引擎使用指南(Android)<二>
- js获取select标签选中的值
- 抓取屏幕,分析屏幕上的目标小图片位置,代码犀利,速度很快
- POJ 3268 Silver Cow Party
- java中List的排序功能的实现
- Spark编程模型
- 拷贝构造函数 copy constructor 语意学
- JAVA 继承与组合
- Launcher2何实现主菜单背景的透明度调整
- JavaScript引擎是单线程运行的,浏览器无论在什么时候都只且只有一个线程在运行JavaScript程序
- Qt数据库:(二)添加MySQL驱动插件