编辑8-31-12:根据以下Joey的评论,请注意您比较的位图的格式。它们可能在跨步中包含填充,这些填充使位图不相等,尽管在像素方向上是等效的。有关更多详细信息,请参见此问题。
阅读有关比较字节数组的问题的答案后,产生了MUCH FASTER方法:使用P /
Invoke和msvcrt中的memcmp API调用。这是代码:
[Dllimport("msvcrt.dll")]private static extern int memcmp(IntPtr b1, IntPtr b2, long count);public static bool CompareMemCmp(Bitmap b1, Bitmap b2){ if ((b1 == null) != (b2 == null)) return false; if (b1.Size != b2.Size) return false; var bd1 = b1.LockBits(new Rectangle(new Point(0, 0), b1.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var bd2 = b2.LockBits(new Rectangle(new Point(0, 0), b2.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); try { IntPtr bd1scan0 = bd1.Scan0; IntPtr bd2scan0 = bd2.Scan0; int stride = bd1.Stride; int len = stride * b1.Height; return memcmp(bd1scan0, bd2scan0, len) == 0; } finally { b1.UnlockBits(bd1); b2.UnlockBits(bd2); }}


