有大量图片,因为有各种格式,所以一律转成Image对象处理。想要找出其中相同的图片有什么类库马。另外可能存在同一个图片尺寸不一致的情况,因此要有一定的误差容错。

解决方案 »

  1.   

     public static int BitmapCompare(Bitmap bitmap1, Bitmap bitmap2)
        {
            int result = 0;   
            if (bitmap1 == null || bitmap2 == null)
                return -1;
            if (bitmap1.Width == bitmap2.Width && bitmap1.Height == bitmap2.Height)
            {
                for (int i = 0; i < bitmap1.Width; i++)
                {
                    for (int j = 0; j < bitmap1.Height; j++)
                    {
                        Color color1 = bitmap1.GetPixel(i, j);
                        Color color2 = bitmap2.GetPixel(i, j);
                        if (color1 != color2)
                        {
                            result = color1.ToArgb() - color2.ToArgb();
                            break;
                        }
                    }
                    if (result != 0)
                        break;
                }
            }
            else if (bitmap1.Width != bitmap2.Width)
            {
                result = bitmap1.Width - bitmap2.Width;
            }
            else if (bitmap1.Height != bitmap2.Height)
            {
                result = bitmap1.Height - bitmap2.Height;
            }
            return result;
        }
        public static int BitmapCompare2(Bitmap bitmap1, Bitmap bitmap2)
        {
            int result = 0;    
            if (bitmap1 == null || bitmap2 == null)
                return -1;
            if (bitmap1.Width == bitmap2.Width && bitmap1.Height == bitmap2.Height)
            {
                BitmapData bmd1 = bitmap1.LockBits(new Rectangle(0, 0, bitmap1.Width, bitmap1.Height), ImageLockMode.ReadOnly, bitmap1.PixelFormat);
                BitmapData bmd2 = bitmap2.LockBits(new Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadOnly, bitmap2.PixelFormat);
                int bytes = bmd1.Stride * bitmap1.Height;
                byte[] buff1 = new byte[bytes];
                byte[] buff2 = new byte[bytes];
                Marshal.Copy(bmd1.Scan0, buff1, 0, Marshal.SizeOf(typeof(byte)) * bytes);
                Marshal.Copy(bmd2.Scan0, buff2, 0, Marshal.SizeOf(typeof(byte)) * bytes);
                result = MemoryCompare(buff1, buff2);
                bitmap1.UnlockBits(bmd1);
                bitmap2.UnlockBits(bmd2);
            }
            else if (bitmap1.Width != bitmap2.Width)
            {
                result = bitmap1.Width - bitmap2.Width;
            }
            else if (bitmap1.Height != bitmap2.Height)
            {
                result = bitmap1.Height - bitmap2.Height;
            }
            return result;
        }
        public static int BitmapCompare3(Bitmap bitmap1, Bitmap bitmap2)
        {
            int result = 0; 
            if (bitmap1 == null || bitmap2 == null)
                return -1;
            if (bitmap1.Width == bitmap2.Width && bitmap1.Height == bitmap2.Height)
            {
                BitmapData bmd1 = bitmap1.LockBits(new Rectangle(0, 0, bitmap1.Width, bitmap1.Height), ImageLockMode.ReadOnly, bitmap1.PixelFormat);
                BitmapData bmd2 = bitmap2.LockBits(new Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadOnly, bitmap2.PixelFormat);
                IntPtr start1 = bmd1.Scan0;
                IntPtr start2 = bmd2.Scan0;
                int sizeOfByte = Marshal.SizeOf(typeof(byte));
                for (int i = 0; i < sizeOfByte * bmd1.Stride * bitmap1.Height; i++)
                {
                    byte b1 = Marshal.ReadByte(start1, i);
                    byte b2 = Marshal.ReadByte(start2, i);
                    if (b1 != b2)
                    {
                        result = (int)(b1 - b2);
                        break;
                    }
                }
                bitmap1.UnlockBits(bmd1);
                bitmap2.UnlockBits(bmd2);
            }
            else if (bitmap1.Width != bitmap2.Width)
            {
                result = bitmap1.Width - bitmap2.Width;
            }
            else if (bitmap1.Height != bitmap2.Height)
            {
                result = bitmap1.Height - bitmap2.Height;
            }
            return result;
        }
    参考