热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

抓取屏幕,分析屏幕上的目标小图片位置,代码犀利,速度很快

原文:http:www.codeproject.comArticles25025Screen-Scraper-in-Managed-CodeScreenScraperinManaged

原文:http://www.codeproject.com/Articles/25025/Screen-Scraper-in-Managed-Code

Screen Scraper in Managed Code

核心代码:

public List findImages()
{
Bitmap bm = getDesktopBitmap();
BitmapData bmd = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height),
ImageLockMode.ReadOnly, bm.PixelFormat);
List results = new List();
foundRects = new List();

for (int y = 0; y {
byte* scanline = (byte*)bmd.Scan0 + (y * bmd.Stride);

for (int x = 0; 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 {
// Horizontal line of pixles in the bitmap data
byte* scanline = (byte*)bmd.Scan0 + ((sy + iy) * bmd.Stride);

for (ix = 0; 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.




推荐阅读
author-avatar
Z-RZI
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有