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

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