(view source code of getdimensions.cs as plain text)
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Shell32;
// Add a reference to "Microsoft Shell Controls and Automation" from the COM tab of the References dialog
namespace RobvanderWoude
{
internal class GetDimensions
{
static readonly string progver = "1.00";
[STAThread]
static int Main( string[] args )
{
bool returnheight = false;
int imgwidth = 0;
int imgheight = 0;
string dimensions = string.Empty;
#region Parse Command Line
foreach ( string arg in args )
{
if ( arg == "/?" )
{
return ShowHelp( );
}
}
if ( args.Length == 0 )
{
return ShowHelp( );
}
if ( args.Length > 2 )
{
return ShowHelp( "Too many command line arguments" );
}
if ( !File.Exists( args[0] ) )
{
return ShowHelp( "File not found: \"{0}\"", args[0] );
}
string parentfolder = Directory.GetParent( args[0] ).FullName;
string imagename = Path.GetFileName( args[0] );
if ( args.Length > 1 )
{
if ( args[1].ToUpper( ) == "/H" )
{
returnheight = true;
}
else
{
return ShowHelp( "Invalid command line argument \"{0}\"", args[1].Trim( "\" ".ToCharArray( ) ) );
}
}
#endregion Parse Command Line
#region Get Image Dimensions
List<string> headers = new List<string>( );
Shell shell = new Shell( );
Folder folder = shell.NameSpace( parentfolder );
FolderItem image = folder.ParseName( imagename );
for ( int i = 0; i < short.MaxValue; i++ )
{
string header = folder.GetDetailsOf( null, i );
if ( string.IsNullOrEmpty( header ) )
{
break;
}
headers.Add( header );
}
for ( int i = 0; i < headers.Count; i++ )
{
if ( headers[i] == "Dimensions" )
{
dimensions = folder.GetDetailsOf( image, i );
}
}
if ( string.IsNullOrWhiteSpace( dimensions ) )
{
return ShowHelp( "Unable to determine the file's dimensions" );
}
string pattern = "(\\d+)\\s*x\\s*(\\d+)";
Regex regex = new Regex( pattern );
if ( !regex.IsMatch( dimensions ) )
{
return ShowHelp( "Unable to determine the file's dimensions" );
}
imgwidth = int.Parse( regex.Matches( dimensions )[0].Groups[1].Value );
imgheight = int.Parse( regex.Matches( dimensions )[0].Groups[2].Value );
#endregion Get Image Dimensions
Console.WriteLine( dimensions );
if ( returnheight )
{
return imgheight;
}
else
{
return imgwidth;
}
}
public static int ShowHelp( params string[] errmsg )
{
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( 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
/*
GetDimensions.exe, Version 1.00
Get the dimensions of an image file
Usage: GetDimensions.exe imagefile [ /H ]
Where: imagefile is the image file whose dimensions we want to know
/H return code equals image Height
(default: return code equals image width)
Notes: This program can get the dimensions for any image type that is
recognized in Windows Explorer.
Return code equals the image width, or with /H switch the image
height, or -1 in case of errors.
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "GetDimensions.exe, Version {0}", progver );
Console.Error.WriteLine( "Get the dimensions of an image file" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "GetDimensions.exe imagefile [ /H ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "imagefile" );
Console.ResetColor( );
Console.Error.WriteLine( " is the image file whose dimensions we want to know" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /H" );
Console.ResetColor( );
Console.Error.Write( " return code equals image " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "H" );
Console.ResetColor( );
Console.Error.WriteLine( "eight" );
Console.Error.WriteLine( " (default: return code equals image width)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: This program can get the dimensions for any image type that is" );
Console.Error.WriteLine( " recognized in Windows Explorer." );
Console.Error.Write( " Return code equals the image width, or with " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/H" );
Console.ResetColor( );
Console.Error.WriteLine( " switch the image" );
Console.Error.WriteLine( " height, or -1 in case of errors." );
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;
}
}
}
page last modified: 2024-04-16; loaded in 0.0068 seconds