Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for compareimages.cs

(view source code of compareimages.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	internal class CompareImages
  10. 	{
  11. 		static readonly string progver = "1.01";
  12.  
  13.  
  14. 		static int Main( string[] args )
  15. 		{
  16. 			if ( args.Length != 2 )
  17. 			{
  18. 				return ShowHelp( );
  19. 			}
  20. 			if ( !File.Exists( args[0] ) )
  21. 			{
  22. 				return ShowHelp( "File \"{0}\" not found", args[0] );
  23. 			}
  24. 			if ( !File.Exists( args[1] ) )
  25. 			{
  26. 				return ShowHelp( "File \"{0}\" not found", args[1] );
  27. 			}
  28.  
  29. 			Bitmap bmp1, bmp2;
  30. 			try
  31. 			{
  32. 				bmp1 = new Bitmap( args[0] );
  33. 				bmp2 = new Bitmap( args[1] );
  34. 			}
  35. 			catch ( Exception e )
  36. 			{
  37. 				return ShowHelp( e.Message );
  38. 			}
  39.  
  40. 			if ( bmp1.Width != bmp2.Width || bmp1.Height != bmp2.Height )
  41. 			{
  42. 				return ShowHelp( "Images have different dimensions" );
  43. 			}
  44.  
  45. 			for ( int i = 0; i < bmp1.Width; i++ )
  46. 			{
  47. 				for ( int j = 0; j < bmp1.Height; j++ )
  48. 				{
  49. 					Color pixel1 = bmp1.GetPixel( i, j );
  50. 					Color pixel2 = bmp2.GetPixel( i, j );
  51. 					if ( pixel1.A != pixel2.A || pixel1.R != pixel2.R || pixel1.G != pixel2.G || pixel1.B != pixel2.B )
  52. 					{
  53. 						Console.ForegroundColor = ConsoleColor.Red;
  54. 						Console.WriteLine( "Images are NOT identical" );
  55. 						Console.ResetColor( );
  56. 						return 1;
  57. 					}
  58. 				}
  59. 			}
  60.  
  61. 			Console.ForegroundColor = ConsoleColor.Green;
  62. 			Console.WriteLine( "Images are identical" );
  63. 			Console.ResetColor( );
  64. 			return 0;
  65. 		}
  66.  
  67.  
  68. 		public static int ShowHelp( params string[] errmsg )
  69. 		{
  70. 			#region Error Message
  71.  
  72. 			if ( errmsg.Length > 0 )
  73. 			{
  74. 				List<string> errargs = new List<string>( errmsg );
  75. 				errargs.RemoveAt( 0 );
  76. 				Console.Error.WriteLine( );
  77. 				Console.ForegroundColor = ConsoleColor.Red;
  78. 				Console.Error.Write( "ERROR:\t" );
  79. 				Console.ForegroundColor = ConsoleColor.White;
  80. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  81. 				Console.ResetColor( );
  82. 			}
  83.  
  84. 			#endregion Error Message
  85.  
  86.  
  87. 			#region Help Text
  88.  
  89. 			/*
  90. 			CompareImages.exe,  Version 1.00
  91. 			Compare 2 images pixel by pixel, ignoring EXIF and IPTC metadata
  92.  
  93. 			Usage:    COMPAREIMAGES.EXE  image1  image2
  94.  
  95. 			Where:    image1, image2     are the images to be compared
  96.  
  97. 			Note:     Return code (\"ErrorLevel\") 0 if images are identical,
  98. 			          1 if they are not, or -1 in case of command line or file
  99. 			          IO errors or if files not recognized as images.
  100.  
  101. 			Written by Rob van der Woude
  102. 			https://www.robvanderwoude.com
  103. 			*/
  104.  
  105. 			#endregion Help Text
  106.  
  107.  
  108. 			#region Display Help Text
  109.  
  110. 			Console.Error.WriteLine( );
  111.  
  112. 			Console.Error.WriteLine( "CompareImages.exe,  Version {0}", progver );
  113.  
  114. 			Console.Error.WriteLine( "Compare 2 images pixel by pixel, ignoring EXIF and IPTC metadata" );
  115.  
  116. 			Console.Error.WriteLine( );
  117.  
  118. 			Console.Error.Write( "Usage:    " );
  119. 			Console.ForegroundColor = ConsoleColor.White;
  120. 			Console.Error.WriteLine( "COMPAREIMAGES.EXE  image1  image2" );
  121. 			Console.ResetColor( );
  122.  
  123. 			Console.Error.WriteLine( );
  124.  
  125. 			Console.Error.Write( "Where:    " );
  126. 			Console.ForegroundColor = ConsoleColor.White;
  127. 			Console.Error.Write( "image1, image2" );
  128. 			Console.ResetColor( );
  129. 			Console.Error.WriteLine( "     are the images to be compared" );
  130.  
  131. 			Console.Error.WriteLine( );
  132.  
  133. 			Console.Error.WriteLine( "Note:     Return code (\"ErrorLevel\") 0 if images are identical," );
  134.  
  135. 			Console.Error.WriteLine( "          1 if they are not, or -1 in case of command line or file" );
  136.  
  137. 			Console.Error.WriteLine( "          IO errors or if files not recognized as images." );
  138.  
  139. 			Console.Error.WriteLine( );
  140.  
  141. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  142.  
  143. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  144.  
  145. 			#endregion Display Help Text
  146.  
  147.  
  148. 			return -1;
  149. 		}
  150. 	}
  151. }

page last modified: 2024-04-16; loaded in 0.0085 seconds