Select an image. The specifications are as follows:
Using System; using System. diagnostics; using System. drawing; using System. drawing. imaging; using System. IO; using System. runtime. interopServices; namespace ConsoleApplication27 {class Program {static void Main (string [] args) {try {Stopwatch _ watch = new Stopwatch (); _ watch. start (); bool _ compareByMenCmpResult = Resource1.cat. compareByMemCmp (Resource1.cat); _ watch. stop (); Console. writeLine (string. format ("CompareByMemCmp: {0} {1}", _ compareByMenCmpResult, _ watch. elapsedMilliseconds); _ watch. reset (); _ watch. start (); bool _ compareByPixel = Resource1.cat. compareByPixel (Resource1.cat); _ watch. stop (); Console. writeLine (string. format ("CompareByPixel: {0} {1}", _ compareByPixel, _ watch. elapsedMilliseconds); _ watch. reset (); _ watch. start (); bool _ compareByBase64String = Resource1.cat. compareByBase64String (Resource1.cat); _ watch. stop (); Console. writeLine (string. format ("CompareByBase64String: {0} {1}", _ compareByBase64String, _ watch. elapsedMilliseconds); _ watch. reset (); _ watch. start (); bool _ compareByArray = Resource1.cat. compareByArray (Resource1.cat); _ watch. stop (); Console. writeLine (string. format ("CompareByArray: {0} {1}", _ compareByArray, _ watch. elapsedMilliseconds);} catch (Exception ex) {Console. writeLine (ex. message);} finally {Console. readLine () ;}} public static class ImageTool {[DllImport ("msvcrt. dll ")] private static extern int memcmp (IntPtr b1, IntPtr b2, long count ); /// <summary> /// Bitmap comparison /// </summary> /// <param name = "b1"> Bitmap1 </param> /// <param name = "b2"> Bitmap2 </param> // <returns> comparison result </returns> public static bool CompareByMemCmp (this Bitmap b1, bitmap b2) {/* Description * Reference link :* http://stackoverflow.com/questions/2031217/what-is-the-fastest-way-i-can-compare-two-equal-size-bitmaps-to-determine-whethe */If (b1 = null )! = (B2 = null) return false; if (b1.Size! = B2.Size) return false; BitmapData _ bdata1 = b1.LockBits (new Rectangle (new Point (0, 0), b1.Size), ImageLockMode. readOnly, PixelFormat. format32bppArgb); BitmapData _ bdata2 = b2.LockBits (new Rectangle (new Point (0, 0), b2.Size), ImageLockMode. readOnly, PixelFormat. format32bppArgb); try {IntPtr _ bd1scan0 = _ bdata1.Scan0; IntPtr _ bd2scan0 = _ bdata2.Scan0; int _ stride = _ bdata1.Stride; int _ len = _ stride * b1.Height; return memcmp (_ bd1scan0, _ bd2scan0, _ len) = 0;} finally {b1.UnlockBits (_ bdata1); b2.UnlockBits (_ bdata2 );}} /// <summary> /// compare the ToBase64String values of bitmap to determine whether they are equal. /// </summary> /// <param name = "b1"> Bitmap1 </param >/// <param name = "b2"> Bitmap2 </param> /// <returns> comparison result </returns> public static bool CompareByBase64String (this Bitmap b1, bitmap b2) {/* Description * Reference link :* http://blogs.msdn.com/b/domgreen/archive/2009/09/06/comparing-two-images-in-c.aspx */String _ b1Base64String, _ b2Base64String; MemoryStream _ ms = new MemoryStream (); try {b1.Save (_ ms, ImageFormat. png); _ b1Base64String = Convert. toBase64String (_ ms. toArray (); _ ms. position = 0; b2.Save (_ ms, ImageFormat. png); _ b2Base64String = Convert. toBase64String (_ ms. toArray ();} finally {_ ms. close ();} return _ b1Base64String. equals (_ b2Base64String );} /// <summary> /// compare the pixel color of bitmap to determine whether the two are equal. // </summary> /// <param name = "b1"> Bitmap1 </param> /// <param name = "b2"> Bitmap2 </param> /// <returns> comparison result </returns> public static bool CompareByPixel (this Bitmap b1, bitmap b2) {/* Description * Reference link :* http://blogs.msdn.com/b/domgreen/archive/2009/09/06/comparing-two-images-in-c.aspx */Bool _ flag = false; if (b1.Width = b2.Width & b1.Height = b2.Height) {_ flag = true; Color _ pixel1; Color _ pixel2; for (int I = 0; I <b1.Width; I ++) {for (int j = 0; j <b1.Height; j ++) {_ pixel1 = b1.GetPixel (I, j); _ pixel2 = b2.GetPixel (I, j); if (_ pixel1! = _ Pixel2) {_ flag = false; break ;}}} return _ flag ;} /// <summary> /// memcmp API /// </summary> /// <param name = "b1"> byte array 1 </param> /// <param name = "b2"> byte array 2 </param> // <returns> If the two arrays are the same, returns 0; If array 1 is less than array 2, returns a value smaller than 0; If array 1 is greater than array 2, returns a value greater than 0. </Returns> [DllImport ("msvcrt. dll ")] private static extern IntPtr memcmp (byte [] b1, byte [] b2, IntPtr count ); /// <summary> /// compare the byte [] of bitmap to determine whether the two are equal. /// </summary> /// <param name = "b1"> Bitmap1 </param> /// <param name = "b2"> Bitmap2 </param> /// <returns> comparison result </returns> public static bool CompareByArray (this Bitmap b1, bitmap b2) {/* Description * Reference link :* http://www.cnblogs.com/zgqys1980/archive/2009/07/13/1522546.html */IntPtr _ result = new IntPtr (-1); MemoryStream _ ms = new MemoryStream (); try {b1.Save (_ ms, ImageFormat. png); byte [] _ b1Array = _ ms. toArray (); _ ms. position = 0; b2.Save (_ ms, ImageFormat. png); byte [] _ b2Array = _ ms. toArray (); _ result = memcmp (_ b1Array, _ b2Array, new IntPtr (_ b1Array. length);} finally {_ ms. close ();} return _ result. toInt32 () = 0 ;}}}Test results:
Hope this is helpful!