sort jpegs by average hue in csharp

Here's a sample code to sort a list of JPEG files in a directory by their average hue value:

main.cs
using System;
using System.Collections.Generic;
using System.Drawing;

class JpegFile
{
    public string Filename { get; set; }
    public double Hue { get; set; }
}

class JpegSorter
{
    static void Main()
    {
        string directoryPath = @"C:\MyJpegDirectory\";

        List<JpegFile> jpegFiles = new List<JpegFile>();

        foreach (string filePath in System.IO.Directory.GetFiles(directoryPath, "*.jpg"))
        {
            Bitmap bitmap = new Bitmap(filePath);

            double totalHue = 0;

            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    Color pixelColor = bitmap.GetPixel(x, y);
                    totalHue += pixelColor.GetHue();
                }
            }

            JpegFile jpegFile = new JpegFile
            {
                Filename = filePath,
                Hue = totalHue / (bitmap.Width * bitmap.Height)
            };

            jpegFiles.Add(jpegFile);
        }

        jpegFiles.Sort((file1, file2) => file1.Hue.CompareTo(file2.Hue));

        foreach (JpegFile jpegFile in jpegFiles)
        {
            Console.WriteLine("{0} : {1}", jpegFile.Filename, jpegFile.Hue);
        }
    }
}
1255 chars
51 lines

The code reads all JPEG files in a directory and computes the average hue value of each image. Then, it sorts the list of JpegFile items by their hue values using a lambda expression. Finally, it displays the list of JPEG filenames and their corresponding average hue values in the sorted order.

gistlibby LogSnag