• 引言

    最近,由于工作上的某些原因,又要写类似于外挂的程序,又要用到一个屏幕找图功能,很多程序(eg:按键精灵)都提供了类似的功能,其实在这之前,我也查找过很多类似的C#方法,因为之前有一个试过没有用得起,所以最后就放弃了,知道现在都是使用的自己写的一个,相对来说,除了效率比较慢,没有太大的问题。不过就是由于效率不高,后面又想了其他的一些解决办法。

  • 基础+贴代码。

    因为是一些图片处理和操作,所以必不可少的会用到C# GDI+的一些基本知识,对于这个网上应该也有很多,大家可以拿来学习和参考。

    再者,其实细细想一下,其实应该很简单,为什么呢,因为就是一个一个像素的比较,比较颜色差异,没有差异就通过,有差异,就继续查找,知道找到必须要,且完全匹配就OK。

    于是乎有了下面的代码。1.0

  // 基础代码和调用代码 (注释基本,略,后面又没有添加,多多包涵)

 public class ImageManager
{
public static Point Compare(Bitmap bigImage, Bitmap smallImage)
{
for (int i = ; i < bigImage.Width; i++)
{
for (int j = ; j < bigImage.Height; j++)
{
Color c1 = bigImage.GetPixel(i, j);
Color c2 = smallImage.GetPixel(, ); // 颜色相等,且没有超出边界
if (Compare(c1, c2) && bigImage.Width >= (i + smallImage.Width) && bigImage.Height >= (j + smallImage.Height))
{
bool iscontinue = false;
for (int x = ; x < smallImage.Width; x++)
{
for (int y = ; y < smallImage.Height; y++)
{
Color c3 = smallImage.GetPixel(x, y);
Color c4 = bigImage.GetPixel(i + x, j + y);
if (!Compare(c3, c4))
{
iscontinue = true;
break;
}
} if (iscontinue)
{
break;
}
} if (!iscontinue)
{
return new Point(i, j);
}
}
}
} return new Point(-, -);
} private static bool Compare(Color c1, Color c2)
{
if (c1.A == c2.A && c1.R == c2.R && c1.B == c2.B && c1.G == c2.G)
{
return true;
} return false;
}
}

C# ImageManager 1.0

     /// <summary>
/// 得到指定图片顶点
/// </summary>
/// <param name="picName">图片名称</param>
private Point GetPicturePoint(string picName)
{
Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image); //设置截屏区域
imgGraphics.CopyFromScreen(, , , , new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)); // 然后从截屏图片中查找指定图片
string taskImagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image", picName);
Image img = Image.FromFile(taskImagePath); var result = ImageManager.Compare(CloseImg(image), CloseImg(img)); return result;
} private Bitmap CloneImg(Image img)
{
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图像以指定的格式存入缓存内存流
byte[] bt = new byte[mostream.Length];
mostream.Position = ;//设置流的初始位置
mostream.Read(bt, , Convert.ToInt32(bt.Length)); return bmp;
}
}

ImageManager 调用方法

  上面的CloseImg麻烦修改成CloneImg 写错了,多谢网友指出。

    由于效率不敢恭维,没办法,又想其他的法子吧,于是乎想到了多线程。。

  • 多线程处理,效率没啥子提升感觉。

    由于代码的处理方式,造成了,循环太多,处理的比较的次数很多,运算量大。。

    多线程怎么处理呢,于是想到了,把整个屏幕分成很多块小图片,这样,用小图片和要查找的图片进行比较然后得到最后的结果。但是问题来了,如果,图片正好在中间怎么办。于是就把小图片,朵切割一点,多切割,需要查找的图片的宽度和高度。

    于是写成了代码,如下:

 public class ImageManager
{
private static List<Point> result = new List<Point>(); public static event Action<int, Image> DoPic; private static int width = ; private static int height = ; /// <summary>
/// 多线程找图
/// </summary>
/// <param name="bigImage"></param>
/// <param name="smallImage"></param>
/// <returns></returns>
public static Point ThreadCompare(Bitmap bigImage, Bitmap smallImage)
{
result = new List<Point>();
// 先拆分大图成为16个小图片,每个小图片都需要加上smallImage的长宽组成一个新图片
// 需要16个线程来完成。
width = (int)Math.Ceiling(bigImage.Width / 4.0);
height = (int)Math.Ceiling(bigImage.Height / 4.0);
int maxWidth = width + smallImage.Width;
int maxHeight = height + smallImage.Height;
int index = ;
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
Bitmap bitMap = null;
if (i == && j == )
{
bitMap = new Bitmap(width, height);
}
else if (j == )
{
bitMap = new Bitmap(maxWidth, height);
}
else if (i == )
{
bitMap = new Bitmap(width, maxWidth);
}
else
{
bitMap = new Bitmap(maxWidth, maxHeight);
} Graphics resultG = Graphics.FromImage(bitMap);
resultG.DrawImage(bigImage, new Rectangle(, , bitMap.Width, bitMap.Height), new Rectangle(i * width, j * height, bitMap.Width, bitMap.Height), GraphicsUnit.Pixel);
resultG.Dispose(); if (DoPic != null)
{
DoPic(index, CloneImg(bitMap));
} ThreadPool.QueueUserWorkItem(new WaitCallback(CompareThread), new object[] { bitMap, CloneImg(smallImage), i, j });
index++;
}
} while (result.Count != )
{
Thread.Sleep();
} var point = new Point(-, -);
if (result.Exists(p => p.X >= ))
{
point = result.Find(a => a.X >= );
} return point;
} public static Point Compare(Bitmap bigImage, Bitmap smallImage)
{
for (int i = ; i < bigImage.Width; i++)
{
for (int j = ; j < bigImage.Height; j++)
{
Color c1 = bigImage.GetPixel(i, j);
Color c2 = smallImage.GetPixel(, ); // 颜色相等,且没有超出边界
if (Compare(c1, c2) && bigImage.Width >= (i + smallImage.Width) && bigImage.Height >= (j + smallImage.Height))
{
bool iscontinue = false;
for (int x = ; x < smallImage.Width; x++)
{
for (int y = ; y < smallImage.Height; y++)
{
Color c3 = smallImage.GetPixel(x, y);
Color c4 = bigImage.GetPixel(i + x, j + y);
if (!Compare(c3, c4))
{
iscontinue = true;
break;
}
} if (iscontinue)
{
break;
}
} if (!iscontinue)
{
return new Point(i, j);
}
}
}
} return new Point(-, -);
} private static void CompareThread(object obj)
{
object[] objs = obj as object[];
Bitmap bigImage = objs[] as Bitmap;
Bitmap smallImage = objs[] as Bitmap;
int indexI = Convert.ToInt32(objs[]);
int indexJ = Convert.ToInt32(objs[]);
bool isbreak = false;
Point p = new Point(-, -);
for (int i = ; i < bigImage.Width; i++)
{
for (int j = ; j < bigImage.Height; j++)
{
Color c1 = bigImage.GetPixel(i, j);
Color c2 = smallImage.GetPixel(, ); // 颜色相等,且没有超出边界
if (Compare(c1, c2) && bigImage.Width >= (i + smallImage.Width) && bigImage.Height >= (j + smallImage.Height))
{
bool iscontinue = false;
for (int x = ; x < smallImage.Width; x++)
{
for (int y = ; y < smallImage.Height; y++)
{
Color c3 = smallImage.GetPixel(x, y);
Color c4 = bigImage.GetPixel(i + x, j + y);
if (!Compare(c3, c4))
{
iscontinue = true;
break;
}
} if (iscontinue)
{
break;
}
} if (!iscontinue)
{
isbreak = true;
p = new Point(i + indexI * width, j + indexJ * height);
break;
}
}
} if (isbreak)
{
break;
}
} result.Add(p);
} private static bool Compare(Color c1, Color c2)
{
if (c1.A == c2.A && c1.R == c2.R && c1.B == c2.B && c1.G == c2.G)
{
return true;
} return false;
} private static Bitmap CloneImg(Image img)
{
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图像以指定的格式存入缓存内存流
byte[] bt = new byte[mostream.Length];
mostream.Position = ;//设置留的初始位置
mostream.Read(bt, , Convert.ToInt32(bt.Length)); return bmp;
}
}
}

ImageManager 2.0

    终于支持多线程了,然后测试了一下,效率略有增加,不过没有太大的感觉。但是用别人的工具,感觉特别快,因为软件上面写的50,60毫秒,我就想啊,到底是哪里拖慢了速度呢。。。当然,没有想到。所以这里就抛砖引玉了。。。

  • 总结

    博客园的编辑器,每次我都感觉自己不会用,别人写的文章,编辑出来效果杠杠的,为什么我这个不行呢,感觉有点坑。

    最后,欢迎拍砖。

    谢谢支持。

C# 之屏幕找图的更多相关文章

  1. Delphi下实现全屏快速找图找色

    前言 最近有好几个朋友都在问我找图找色的问题,奇怪?于是乎写了一个专门用于找图找色的单元文件“BitmapData.pas”.在这个单元文件中我实现了从文件中导入位图.屏幕截图.鼠标指针截图.在图片上 ...

  2. C#实现按键精灵的'找图' '找色' '找字'的功能

    http://www.cnblogs.com/JimmyBright/p/4355862.html 背景:游戏辅助功能通常使用按键精灵编写脚本,按键精灵的最大卖点就是能够找到画面中字,图,色,这对于模 ...

  3. Python实现按键精灵(二)-找图找色

    一.实现功能 判断在指定坐标范围内,是否存在相似度大于n的图片,并返回坐标. 二.基本思路 A=你需要寻找的图片 B=截取当前页面中指定范围的图片 利用opencv 判断A在B中的位置, 在该位置截取 ...

  4. C#实现按键精灵的'找图' '找色' '找字'的功能

    背景:游戏辅助功能通常使用按键精灵编写脚本,按键精灵的最大卖点就是能够找到画面中字,图,色,这对于模拟用户鼠标操作至关重要,这能找到道具,找到血量,实现自动打怪,自动补血,自动买卖道具,博主闲来无聊, ...

  5. python从图片中找图

    import aircv as ac def matcha(bb,aa):#从bb查找aa,如果有则返回其坐标位置 yuan=ac.imread(bb) mubi=ac.imread(aa) resu ...

  6. 第三篇T语言实例开发,图色操作

    ---恢复内容开始--- 图色的基本操作 1.找颜色色命令的基本操作 坐标点取色:获取指定坐标点的颜色 区域找色:在指定区域里找某一个颜色 模糊找色:在指定区域里找某一个颜色,可以设置相似度 多点找色 ...

  7. 屏幕分辨率与FPS

    屏幕分辨率 刷新率分为垂直刷新率和水平刷新率,一般提到的刷新率通常指垂直刷新率. 垂直刷新率表示屏幕的图象每秒钟重绘多少次,也就是每秒钟屏幕刷新的次数,以Hz(赫兹)为单位. 刷新率越高越好,图象就越 ...

  8. [转]SAP中找表的方法

    http://blog.chinaunix.net/uid-24063584-id-2642334.html 分类: 18种根据屏幕字段查找数据库表数据的技巧 帮助   18种根据屏幕字段查找潜在数据 ...

  9. 在屏幕上搜索图片并返回图片所在位置的坐标的AutoHotkey脚本源代码(类似大漠插件)

    ;~  在屏幕上搜索图片并返回图片所在位置的坐标的AutoHotkey脚本源代码(类似大漠插件) ; https://www.autohotkey.com/boards/viewtopic.php?t ...

随机推荐

  1. 我的常用mixin 之 lines

    /** * 最多显示 $lineCount 行 * lines * * example: * @include lines; * @include lines(3); */ @mixin lines( ...

  2. WCF客户端承载

    http://www.cnblogs.com/wengyuli/archive/2010/12/27/1918109.html参考 Hi victory,   你提出的问题很好,这个问题,相信很多人学 ...

  3. Django模块学习- django-pagination

    实在是很简单的一个Django 的分页插件. 使用pip instal pagination 即可完成安装. 完成后配置如下: 1. 将安装文件中的 pagination 文件夹拷贝到项目的根目录下 ...

  4. 关于typedef的使用方法

    在计算机编程语言中用来为复杂的声明定义简单的别名.与宏定义有些差异.它本身是一种存储类的keyword,与auto.extern.mutable.static.register等keyword不能出如 ...

  5. 查看JAVA进程中哪个线程CPU消耗最高

    一,在centos linux 上查看进程占用cpu过高 top  shift+h 查看哪个进程程消耗最高     二,查看JAVA进程中哪个线程消耗最高   2.1 导出java运行的线程信息   ...

  6. “HTTPS”安全在哪里?

    背景 最近基于兴趣学学习了下 HTTPS 相关的知识,在此记录下学习心得. 在上网获取信息的过程中,我们接触最多的信息加密传输方式也莫过于 HTTPS 了.每当访问一个站点,浏览器的地址栏中出现绿色图 ...

  7. dell新服务器安装系统

    公司新采购的dell 630服务器,但是第一次安装操作系统的时候比较麻烦,每次都要重新琢磨下. 现在记录一下,以供下次参考 1.插入服务器自带光盘,设置CD启动,选择部署OS 2.配置raid,然后插 ...

  8. Window中调试HBase问题小结

    1.好久没用log4j了,转到logback好多年了,hbase程序运行时,报缺少log4j配置,那么,就转去logback吧(以下的XXX表示版本号). 原先lib包里面有log4j-XXX.jar ...

  9. 如何在wp8 中调试cocos2dx c++ 代码

    有的时候在win32上运行良好的cocos2dx程序移植到wp8的时候就出了问题,我们想把断点放到c++代码中,需要设置一下VS 2012 右击项目属性 把ui任务 设置为仅限本机 即可.

  10. 理解RxJava:(一)基础知识

    理解RxJava:(一)基础知识 本文翻译自Grokking RxJava, Part 1: The Basics,著作权归原作者danlew所有.译文由JohnTsai翻译.转载请注明出处,并保留此 ...