public static bool FindBitmapLockbits(Bitmap bmpNeedle, Bitmap bmpHaystack, int tolerance, out Point location) { LockBitmap lockNeedle = new LockBitmap(bmpNeedle); LockBitmap lockHaystack = new LockBitmap(bmpHaystack); lockNeedle.LockBits(); lockHaystack.LockBits(); int width = bmpHaystack.Width - bmpNeedle.Width; int height = bmpHaystack.Height - bmpNeedle.Height; for (int outerX = 0; outerX < lockHaystack.Width; ++outerX) { for (int outerY = 0; outerY < lockHaystack.Height; ++outerY) { for (int innerX = 0; innerX < bmpNeedle.Width; ++innerX) { for (int innerY = 0; innerY < bmpNeedle.Height; ++innerY) { Color cNeedle = lockNeedle.GetPixel(innerX, innerY); Color cHaystack = lockHaystack.GetPixel(innerX + outerX, innerY + outerY); if (!AreColorsSimilar(cNeedle, cHaystack, tolerance)) { goto notFound; } } } location = new Point(outerX, outerY); return(true); notFound: continue; } } location = Point.Empty; lockNeedle.UnlockBits(); lockHaystack.UnlockBits(); return(false); }
public static bool FindBitmapLockbits(Bitmap bmpNeedle, Bitmap bmpHaystack, int tolerance, out Point location) { LockBitmap lockNeedle = new LockBitmap(bmpNeedle); LockBitmap lockHaystack = new LockBitmap(bmpHaystack); lockNeedle.LockBits(); lockHaystack.LockBits(); int width = bmpHaystack.Width - bmpNeedle.Width; int height = bmpHaystack.Height - bmpNeedle.Height; for (int outerX = 0; outerX < lockHaystack.Width; ++outerX) { for (int outerY = 0; outerY < lockHaystack.Height; ++outerY) { for (int innerX = 0; innerX < bmpNeedle.Width; ++innerX) { for (int innerY = 0; innerY < bmpNeedle.Height; ++innerY) { Color cNeedle = lockNeedle.GetPixel(innerX, innerY); Color cHaystack = lockHaystack.GetPixel(innerX + outerX, innerY + outerY); if (!AreColorsSimilar(cNeedle, cHaystack, tolerance)) { goto notFound; } } } location = new Point(outerX, outerY); return true; notFound: continue; } } location = Point.Empty; lockNeedle.UnlockBits(); lockHaystack.UnlockBits(); return false; }