C#外掛QQ找茬輔助源碼,早期開發

這是一款幾年前開發的工具,當年做爲一民IT純屌,爲了當年本身心目中的一位女神熬夜開發完成。女神使用後找茬等級瞬間從眼明手快升級爲三隻眼。。。每次看到這個就會想起那段屌絲與女神的回憶。今天特意把代碼更新了一下,支持最新版的《你們來找茬》。下面上運行的截圖,源碼在本文最後(對不起了,與我對戰的那位玩家)html

這個工具的主要運行流程很簡單:遊戲截圖-》比較圖片-》標記圖片不一樣點
記得當時處理程序截圖和比較圖片的時候比較麻煩,下面是截圖的處理類ScreenCapture:
windows

  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:ide

/// <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函數

本文來自放肆雷特 | 胖子的技術博客 歡迎關注胖子的新浪博客@大胖子蜀黍工具

相關文章
相關標籤/搜索