using System; using System.Collections.Generic; using System.Drawing; using System.IO; namespace RobvanderWoude { internal class CompareImages { static readonly string progver = "1.01"; static int Main( string[] args ) { if ( args.Length != 2 ) { return ShowHelp( ); } if ( !File.Exists( args[0] ) ) { return ShowHelp( "File \"{0}\" not found", args[0] ); } if ( !File.Exists( args[1] ) ) { return ShowHelp( "File \"{0}\" not found", args[1] ); } Bitmap bmp1, bmp2; try { bmp1 = new Bitmap( args[0] ); bmp2 = new Bitmap( args[1] ); } catch ( Exception e ) { return ShowHelp( e.Message ); } if ( bmp1.Width != bmp2.Width || bmp1.Height != bmp2.Height ) { return ShowHelp( "Images have different dimensions" ); } for ( int i = 0; i < bmp1.Width; i++ ) { for ( int j = 0; j < bmp1.Height; j++ ) { Color pixel1 = bmp1.GetPixel( i, j ); Color pixel2 = bmp2.GetPixel( i, j ); if ( pixel1.A != pixel2.A || pixel1.R != pixel2.R || pixel1.G != pixel2.G || pixel1.B != pixel2.B ) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine( "Images are NOT identical" ); Console.ResetColor( ); return 1; } } } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine( "Images are identical" ); Console.ResetColor( ); return 0; } public static int ShowHelp( params string[] errmsg ) { #region Error Message if ( errmsg.Length > 0 ) { List errargs = new List( errmsg ); errargs.RemoveAt( 0 ); Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) ); Console.ResetColor( ); } #endregion Error Message #region Help Text /* CompareImages.exe, Version 1.00 Compare 2 images pixel by pixel, ignoring EXIF and IPTC metadata Usage: COMPAREIMAGES.EXE image1 image2 Where: image1, image2 are the images to be compared Note: Return code (\"ErrorLevel\") 0 if images are identical, 1 if they are not, or -1 in case of command line or file IO errors or if files not recognized as images. Written by Rob van der Woude https://www.robvanderwoude.com */ #endregion Help Text #region Display Help Text Console.Error.WriteLine( ); Console.Error.WriteLine( "CompareImages.exe, Version {0}", progver ); Console.Error.WriteLine( "Compare 2 images pixel by pixel, ignoring EXIF and IPTC metadata" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "COMPAREIMAGES.EXE image1 image2" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "image1, image2" ); Console.ResetColor( ); Console.Error.WriteLine( " are the images to be compared" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Note: Return code (\"ErrorLevel\") 0 if images are identical," ); Console.Error.WriteLine( " 1 if they are not, or -1 in case of command line or file" ); Console.Error.WriteLine( " IO errors or if files not recognized as images." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "https://www.robvanderwoude.com" ); #endregion Display Help Text return -1; } } }