这是一款几年前开发的工具,当年作为一民IT纯屌,为了当年自己心目中的一位女神熬夜开发完成。女神使用后找茬等级瞬间从眼明手快升级为三只眼。。。每次看到这个就会想起那段屌丝与女神的回忆。今天特地把代码更新了一下,支持最新版的《大家来找茬》。下面上运行的截图,源码在本文最后:(对不起了,与我对战的那位玩家)
这个工具的主要运行流程很简单:游戏截图-》比较图片-》标记图片不同点
记得当时处理程序截图和比较图片的时候比较麻烦,下面是截图的处理类ScreenCapture:
1 /// <summary> 2 /// 提供全屏和指定窗口的截图 以及保存为文件的类 3 /// </summary> 4 public class ScreenCapture 5 { 6 /// <summary> 7 /// 全屏截图 8 /// </summary> 9 /// <returns></returns> 10 public Image CaptureScreen() 11 { 12 return CaptureWindow(User32.GetDesktopWindow()); 13 } 14 /// <summary> 15 /// 指定窗口截图 16 /// </summary> 17 /// <param name="handle">窗口句柄. (在windows应用程序中, 从Handle属性获得)</param> 18 /// <returns></returns> 19 public Image CaptureWindow(IntPtr handle) 20 { 21 IntPtr hdcSrc = User32.GetWindowDC(handle); 22 User32.RECT windowRect = new User32.RECT(); 23 User32.GetWindowRect(handle, ref windowRect); 24 int width = windowRect.right - windowRect.left; 25 int height = windowRect.bottom - windowRect.top; 26 IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); 27 IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); 28 IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); 29 GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); 30 GDI32.SelectObject(hdcDest, hOld); 31 GDI32.DeleteDC(hdcDest); 32 User32.ReleaseDC(handle, hdcSrc); 33 Image img = Image.FromHbitmap(hBitmap); 34 GDI32.DeleteObject(hBitmap); 35 return img; 36 } 37 /// <summary> 38 /// 指定窗口截图 保存为图片文件 39 /// </summary> 40 /// <param name="handle"></param> 41 /// <param name="filename"></param> 42 /// <param name="format"></param> 43 public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format) 44 { 45 Image img = CaptureWindow(handle); 46 img.Save(filename, format); 47 } 48 /// <summary> 49 /// 全屏截图 保存为文件 50 /// </summary> 51 /// <param name="filename"></param> 52 /// <param name="format"></param> 53 public void CaptureScreenToFile(string filename, ImageFormat format) 54 { 55 Image img = CaptureScreen(); 56 img.Save(filename, format); 57 } 58 59 /// <summary> 60 /// 辅助类 定义 Gdi32 API 函数 61 /// </summary> 62 private class GDI32 63 { 64 65 public const int SRCCOPY = 0x00CC0020; 66 [DllImport("gdi32.dll")] 67 public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, 68 int nWidth, int nHeight, IntPtr hObjectSource, 69 int nXSrc, int nYSrc, int dwRop); 70 [DllImport("gdi32.dll")] 71 public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, 72 int nHeight); 73 [DllImport("gdi32.dll")] 74 public static extern IntPtr CreateCompatibleDC(IntPtr hDC); 75 [DllImport("gdi32.dll")] 76 public static extern bool DeleteDC(IntPtr hDC); 77 [DllImport("gdi32.dll")] 78 public static extern bool DeleteObject(IntPtr hObject); 79 [DllImport("gdi32.dll")] 80 public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); 81 } 82 83 /// <summary> 84 /// 辅助类 定义User32 API函数 85 /// </summary> 86 private class User32 87 { 88 [StructLayout(LayoutKind.Sequential)] 89 public struct RECT 90 { 91 public int left; 92 public int top; 93 public int right; 94 public int bottom; 95 } 96 [DllImport("user32.dll")] 97 public static extern IntPtr GetDesktopWindow(); 98 [DllImport("user32.dll")] 99 public static extern IntPtr GetWindowDC(IntPtr hWnd); 100 [DllImport("user32.dll")] 101 public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC); 102 [DllImport("user32.dll")] 103 public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); 104 } 105 }
图片比较类ImageComparer:
/// <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
本文来自放肆雷特 | 胖子的技术博客 欢迎关注胖子的新浪博客@大胖子蜀黍