这是一款几年前开发的工具,当年作为一民IT纯屌,为了当年自己心目中的一位女神熬夜开发完成。女神使用后找茬等级瞬间从眼明手快升级为三只眼。。。每次看到这个就会想起那段屌丝与女神的回忆。今天特地把代码更新了一下,支持最新版的《大家来找茬》。下面上运行的截图,源码在本文最后:(对不起了,与我对战的那位玩家)
这个工具的主要运行流程很简单:游戏截图-》比较图片-》标记图片不同点
记得当时处理程序截图和比较图片的时候比较麻烦,下面是截图的处理类ScreenCapture:
[C#] 纯文本查看 复制代码 /// <summary>
/// 提供全屏和指定窗口的截图 以及保存为文件的类
/// </summary>
public class ScreenCapture
{
/// <summary>
/// 全屏截图
/// </summary>
/// <returns></returns>
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
/// <summary>
/// 指定窗口截图
/// </summary>
/// <param name="handle">窗口句柄. (在windows应用程序中, 从Handle属性获得)</param>
/// <returns></returns>
public Image CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = User32.GetWindowDC(handle);
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
GDI32.SelectObject(hdcDest, hOld);
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
Image img = Image.FromHbitmap(hBitmap);
GDI32.DeleteObject(hBitmap);
return img;
}
/// <summary>
/// 指定窗口截图 保存为图片文件
/// </summary>
/// <param name="handle"></param>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename, format);
}
/// <summary>
/// 全屏截图 保存为文件
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename, format);
}
/// <summary>
/// 辅助类 定义 Gdi32 API 函数
/// </summary>
private class GDI32
{
public const int SRCCOPY = 0x00CC0020;
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
/// <summary>
/// 辅助类 定义User32 API函数
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
}
}
图片比较类ImageComparer:
[C#] 纯文本查看 复制代码 /// <summary>
/// 图像比较.用于找出两副图片之间的差异位置
/// </summary>
public class ImageComparer
{
/// <summary>
/// 图像颜色
/// </summary>
[StructLayout(LayoutKind.Explicit)]
private struct ICColor
{
[FieldOffset(0)]
public byte B;
[FieldOffset(1)]
public byte G;
[FieldOffset(2)]
public byte R;
}
/// <summary>
/// 按5*5大小进行分块比较两个图像.
/// </summary>
/// <param name="bmp1"></param>
/// <param name="bmp2"></param>
/// <returns></returns>
public static List<Rectangle> Compare(Bitmap bmp1, Bitmap bmp2)
{
return Compare(bmp1, bmp2, new Size(5, 5));
}
/// <summary>
/// 比较两个图像
/// </summary>
/// <param name="bmp1"></param>
/// <param name="bmp2"></param>
/// <param name="block"></param>
/// <returns></returns>
public static List<Rectangle> Compare(Bitmap bmp1, Bitmap bmp2, Size block)
{
List<Rectangle> rects = new List<Rectangle>();
PixelFormat pf = PixelFormat.Format24bppRgb;//5+1+a+s+p+x
BitmapData bd1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), ImageLockMode.ReadOnly, pf);
BitmapData bd2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadOnly, pf);
try
{
unsafe
{
int w = 0, h = 0;
while (h < bd1.Height && h < bd2.Height)
{
byte* p1 = (byte*)bd1.Scan0 + h * bd1.Stride;
byte* p2 = (byte*)bd2.Scan0 + h * bd2.Stride;
w = 0;
while (w < bd1.Width && w < bd2.Width)
{
//按块大小进行扫描
for (int i = 0; i < block.Width; i++)
{
int wi = w + i;
if (wi >= bd1.Width || wi >= bd2.Width) break;
for (int j = 0; j < block.Height; j++)
{
int hj = h + j;
if (hj >= bd1.Height || hj >= bd2.Height) break;
ICColor* pc1 = (ICColor*)(p1 + wi * 3 + bd1.Stride * j);
ICColor* pc2 = (ICColor*)(p2 + wi * 3 + bd2.Stride * j);
if (pc1->R != pc2->R || pc1->G != pc2->G || pc1->B != pc2->B)
{
//当前块有某个象素点颜色值不相同.也就是有差异.
int bw = Math.Min(block.Width, bd1.Width - w);
int bh = Math.Min(block.Height, bd1.Height - h);
rects.Add(new Rectangle(w, h, bw, bh));
goto E;
}
}
}
E:
w += block.Width;
}
h += block.Height;
}
}
}
finally
{
bmp1.UnlockBits(bd1);
bmp2.UnlockBits(bd2);
}
return rects;
}
}
QQ找茬辅助源码下载:http://www.fangsi.net/archives/742.html 本文来自放肆雷特 | 胖子的技术博客 欢迎关注胖子的新浪博客@大胖子蜀黍
QQZhaoCha.zip
(84.71 KB, 下载次数: 286)
|