Click here to Skip to main content
15,895,656 members
Articles / Programming Languages / C#

Reading Image Headers to Get Width and Height

Rate me:
Please Sign up or sign in to vote.
4.97/5 (16 votes)
28 Apr 2009CPOL3 min read 126.2K   2.6K   44  
Looks at techniques for getting an image's width and height quickly
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DevWilson
{
    class Tests
    {
        public static int Test1(string imagesFolder)
        {
            List<string> imagePaths = new List<string>(Directory.GetFiles(imagesFolder, "*.jpg", SearchOption.AllDirectories));

            ImageOrientationSlow slow = new ImageOrientationSlow();
            List<string> landscapes = new List<string>();

            foreach (string imagePath in imagePaths)
            {
                if (slow.IsLandscape(imagePath))
                {
                    landscapes.Add(imagePath);
                }
            }

            return landscapes.Count;
        }

        public static int Test2(string imagesFolder)
        {
            List<string> imagePaths = new List<string>(Directory.GetFiles(imagesFolder, "*.jpg", SearchOption.AllDirectories));

            ImageOrientationThreaded threaded = new ImageOrientationThreaded();

            threaded.GetLandscapeThreaded(imagePaths);
            List<string> landscapes = threaded.Landscapes;

            return landscapes.Count;
        }

        public static int Test3(string imagesFolder, string cachePath)
        {
            ImageList images = new ImageList(cachePath);

            if (File.Exists(cachePath))
            {
                ImageListToXml.LoadFromXml(cachePath, images);
            }

            ImageFolderQuickCheck.QuickCheck(images, imagesFolder);

            return images.Count(i => i.Orientation == ImageOrientation.Landscape);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Play.com
United Kingdom United Kingdom
Hi, my name's Andy Wilson and I live in Cambridge, UK where I work as a Senior C# Software Developer.

Comments and Discussions