C# code to retrieve image dimensions

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
            string path = @"C:\Users\saikatd\Test";
            if (File.Exists(path))
            {
                // This path is a file
                ProcessFile(path);
            }
            else if (Directory.Exists(path))
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }
        }


        // Process all files in the directory passed in, recurse on any directories 
        // that are found, and process the files they contain.
        public static void ProcessDirectory(string targetDirectory)
        {
            // Process the list of files found in the directory.
            string[] fileEntries = Directory.GetFiles(targetDirectory);
            foreach (string fileName in fileEntries)
                ProcessFile(fileName);

            // Recurse into subdirectories of this directory.
            string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
            foreach (string subdirectory in subdirectoryEntries)
                ProcessDirectory(subdirectory);
        }

        // Insert logic for processing found files here.
        public static void ProcessFile(string path)
        {
            Image img = Image.FromFile(path);
            if(img.Width != 3088 || img.Height != 2056) //Canon 5MP Default Resolution
            {
                Console.WriteLine("Path: " + path);
                Console.WriteLine("Width: " + img.Width + ", Height: " + img.Height);
            }
            img.Dispose();
        }
    }
}

Output
=======
Path: C:\Users\saikatd\Test\IMG_0323.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9966.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9969.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9971.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9977.jpg
Width: 2935, Height: 1954
Path: C:\Users\saikatd\Test\IMG_9981.jpg
Width: 3009, Height: 2003
Path: C:\Users\saikatd\Test\IMG_9984.JPG
Width: 3031, Height: 2056
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s