QQ找茬外挂,用C#代码编写。
使用方法
这个工具的主要运行流程很简单:
游戏截图-》比较图片-》标记图片不同点。
实现代码
截图的处理类ScreenCapture:
///
/// 提供全屏和指定窗口的截图 以及保存为文件的类
///
public class ScreenCapture
{
///
/// 全屏截图
///
///
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
///
/// 指定窗口截图
///
/// 窗口句柄. (在windows应用程序中, 从Handle属性获得)
///
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, , , width, height, hdcSrc, , , GDI32.SRCCOPY);
GDI32.SelectObject(hdcDest, hOld);
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
Image img = Image.FromHbitmap(hBitmap);
GDI32.DeleteObject(hBitmap);
return img;
}
///
/// 指定窗口截图 保存为图片文件
///
///
///
///
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename, format);
}
///
/// 全屏截图 保存为文件
///
///
///
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename, format);
} ///
/// 辅助类 定义 Gdi32 API 函数
///
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);
} ///
/// 辅助类 定义User32 API函数
///
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:
///
/// 图像比较.用于找出两副图片之间的差异位置
///
public class ImageComparer
{
///
/// 图像颜色
///
[StructLayout(LayoutKind.Explicit)]
private struct ICColor
{
[FieldOffset()]
public byte B;
[FieldOffset()]
public byte G;
[FieldOffset()]
public byte R;
} ///
/// 按5*5大小进行分块比较两个图像.
///
///
///
///
public static List Compare(Bitmap bmp1, Bitmap bmp2)
{
return Compare(bmp1, bmp2, new Size(, ));
}
///
/// 比较两个图像
///
///
///
///
///
public static List Compare(Bitmap bmp1, Bitmap bmp2, Size block)
{
List rects = new List();
PixelFormat pf = PixelFormat.Format24bppRgb;//5+1+a+s+p+x BitmapData bd1 = bmp1.LockBits(new Rectangle(, , bmp1.Width, bmp1.Height), ImageLockMode.ReadOnly, pf);
BitmapData bd2 = bmp2.LockBits(new Rectangle(, , bmp2.Width, bmp2.Height), ImageLockMode.ReadOnly, pf); try
{
unsafe
{
int w = , h = ; while (h < bd1.Height && h < bd2.Height)
{
byte* p1 = (byte*)bd1.Scan0 + h * bd1.Stride;
byte* p2 = (byte*)bd2.Scan0 + h * bd2.Stride; w = ;
while (w < bd1.Width && w < bd2.Width)
{
//按块大小进行扫描
for (int i = ; i < block.Width; i++)
{
int wi = w + i;
if (wi >= bd1.Width || wi >= bd2.Width) break; for (int j = ; j < block.Height; j++)
{
int hj = h + j;
if (hj >= bd1.Height || hj >= bd2.Height) break; ICColor* pc1 = (ICColor*)(p1 + wi * + bd1.Stride * j);
ICColor* pc2 = (ICColor*)(p2 + wi * + 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;
}
}
源码和软件下载
http://pan.baidu.com/s/17tGEM