Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for getdimensions.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using Shell32;
  6.  
  7. // Add a reference to "Microsoft Shell Controls and Automation" from the COM tab of the References dialog
  8.  
  9. namespace RobvanderWoude
  10. {
  11. 	internal class GetDimensions
  12. 	{
  13. 		static readonly string progver = "1.00";
  14.  
  15.  
  16. 		[STAThread]
  17. 		static int Main( string[] args )
  18. 		{
  19. 			bool returnheight = false;
  20. 			int imgwidth = 0;
  21. 			int imgheight = 0;
  22. 			string dimensions = string.Empty;
  23.  
  24.  
  25. 			#region Parse Command Line
  26.  
  27. 			foreach ( string arg in args )
  28. 			{
  29. 				if ( arg == "/?" )
  30. 				{
  31. 					return ShowHelp( );
  32. 				}
  33. 			}
  34.  
  35. 			if ( args.Length == 0 )
  36. 			{
  37. 				return ShowHelp( );
  38. 			}
  39.  
  40. 			if ( args.Length > 2 )
  41. 			{
  42. 				return ShowHelp( "Too many command line arguments" );
  43. 			}
  44.  
  45. 			if ( !File.Exists( args[0] ) )
  46. 			{
  47. 				return ShowHelp( "File not found: \"{0}\"", args[0] );
  48. 			}
  49.  
  50. 			string parentfolder = Directory.GetParent( args[0] ).FullName;
  51. 			string imagename = Path.GetFileName( args[0] );
  52.  
  53. 			if ( args.Length > 1 )
  54. 			{
  55. 				if ( args[1].ToUpper( ) == "/H" )
  56. 				{
  57. 					returnheight = true;
  58. 				}
  59. 				else
  60. 				{
  61. 					return ShowHelp( "Invalid command line argument \"{0}\"", args[1].Trim( "\" ".ToCharArray( ) ) );
  62. 				}
  63. 			}
  64.  
  65. 			#endregion Parse Command Line
  66.  
  67.  
  68. 			#region Get Image Dimensions
  69.  
  70. 			List<string> headers = new List<string>( );
  71. 			Shell shell = new Shell( );
  72. 			Folder folder = shell.NameSpace( parentfolder );
  73. 			FolderItem image = folder.ParseName( imagename );
  74. 			for ( int i = 0; i < short.MaxValue; i++ )
  75. 			{
  76. 				string header = folder.GetDetailsOf( null, i );
  77. 				if ( string.IsNullOrEmpty( header ) )
  78. 				{
  79. 					break;
  80. 				}
  81. 				headers.Add( header );
  82. 			}
  83.  
  84. 			for ( int i = 0; i < headers.Count; i++ )
  85. 			{
  86. 				if ( headers[i] == "Dimensions" )
  87. 				{
  88. 					dimensions = folder.GetDetailsOf( image, i );
  89. 				}
  90. 			}
  91. 			if ( string.IsNullOrWhiteSpace( dimensions ) )
  92. 			{
  93. 				return ShowHelp( "Unable to determine the file's dimensions" );
  94. 			}
  95. 			string pattern = "(\\d+)\\s*x\\s*(\\d+)";
  96. 			Regex regex = new Regex( pattern );
  97. 			if ( !regex.IsMatch( dimensions ) )
  98. 			{
  99. 				return ShowHelp( "Unable to determine the file's dimensions" );
  100. 			}
  101. 			imgwidth = int.Parse( regex.Matches( dimensions )[0].Groups[1].Value );
  102. 			imgheight = int.Parse( regex.Matches( dimensions )[0].Groups[2].Value );
  103.  
  104. 			#endregion Get Image Dimensions
  105.  
  106.  
  107. 			Console.WriteLine( dimensions );
  108.  
  109. 			if ( returnheight )
  110. 			{
  111. 				return imgheight;
  112. 			}
  113. 			else
  114. 			{
  115. 				return imgwidth;
  116. 			}
  117. 		}
  118.  
  119.  
  120. 		public static int ShowHelp( params string[] errmsg )
  121. 		{
  122. 			#region Error Message
  123.  
  124. 			if ( errmsg.Length > 0 )
  125. 			{
  126. 				List<string> errargs = new List<string>( errmsg );
  127. 				errargs.RemoveAt( 0 );
  128. 				Console.Error.WriteLine( );
  129. 				Console.ForegroundColor = ConsoleColor.Red;
  130. 				Console.Error.Write( "ERROR:\t" );
  131. 				Console.ForegroundColor = ConsoleColor.White;
  132. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  133. 				Console.ResetColor( );
  134. 			}
  135.  
  136. 			#endregion Error Message
  137.  
  138.  
  139. 			#region Help Text
  140.  
  141. 			/*
  142. 			GetDimensions.exe,  Version 1.00
  143. 			Get the dimensions of an image file
  144.  
  145. 			Usage:    GetDimensions.exe  imagefile  [ /H ]
  146.  
  147. 			Where:    imagefile     is the image file whose dimensions we want to know
  148. 					  /H            return code equals image Height
  149. 			                        (default: return code equals image width)
  150.  
  151. 			Notes:    This program can get the dimensions for any image type that is
  152. 			          recognized in Windows Explorer.
  153. 			          Return code equals the image width, or with /H switch the image
  154. 			          height, or -1 in case of errors.
  155.  
  156. 			Written by Rob van der Woude
  157. 			https://www.robvanderwoude.com
  158. 			*/
  159.  
  160. 			#endregion Help Text
  161.  
  162.  
  163. 			#region Display Help Text
  164.  
  165. 			Console.Error.WriteLine( );
  166.  
  167. 			Console.Error.WriteLine( "GetDimensions.exe,  Version {0}", progver );
  168.  
  169. 			Console.Error.WriteLine( "Get the dimensions of an image file" );
  170.  
  171. 			Console.Error.WriteLine( );
  172.  
  173. 			Console.Error.Write( "Usage:    " );
  174. 			Console.ForegroundColor = ConsoleColor.White;
  175. 			Console.Error.WriteLine( "GetDimensions.exe  imagefile  [ /H ]" );
  176. 			Console.ResetColor( );
  177.  
  178. 			Console.Error.WriteLine( );
  179.  
  180. 			Console.Error.Write( "Where:    " );
  181. 			Console.ForegroundColor = ConsoleColor.White;
  182. 			Console.Error.Write( "imagefile" );
  183. 			Console.ResetColor( );
  184. 			Console.Error.WriteLine( "     is the image file whose dimensions we want to know" );
  185.  
  186. 			Console.ForegroundColor = ConsoleColor.White;
  187. 			Console.Error.Write( "          /H" );
  188. 			Console.ResetColor( );
  189. 			Console.Error.Write( "            return code equals image " );
  190. 			Console.ForegroundColor = ConsoleColor.White;
  191. 			Console.Error.Write( "H" );
  192. 			Console.ResetColor( );
  193. 			Console.Error.WriteLine( "eight" );
  194.  
  195. 			Console.Error.WriteLine( "                        (default: return code equals image width)" );
  196.  
  197. 			Console.Error.WriteLine( );
  198.  
  199. 			Console.Error.WriteLine( "Notes:    This program can get the dimensions for any image type that is" );
  200.  
  201. 			Console.Error.WriteLine( "          recognized in Windows Explorer." );
  202.  
  203. 			Console.Error.Write( "          Return code equals the image width, or with " );
  204. 			Console.ForegroundColor = ConsoleColor.White;
  205. 			Console.Error.Write( "/H" );
  206. 			Console.ResetColor( );
  207. 			Console.Error.WriteLine( " switch the image" );
  208.  
  209. 			Console.Error.WriteLine( "          height, or -1 in case of errors." );
  210.  
  211. 			Console.Error.WriteLine( );
  212.  
  213. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  214.  
  215. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  216.  
  217. 			#endregion Display Help Text
  218.  
  219.  
  220. 			return -1;
  221. 		}
  222. 	}
  223. }

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