Amazon.com Widgets

Very Simple .NET Thumbnail Creation Code

When I was working the update for my Ajax demo, I needed to create thumb nail from a director of photos.  There are tons of tools out there to do this, but I thought I'd share the very simple code I used. 

It takes all the jpgs in the root path and creates 160x120 thumbnails of them. It also copies the original photo into fullpath. 

namespace ThumbNailer
{
    class Program
    {
        static void Main(string[] args)
        {
            string rootPath = @"C:\Users\brada\Desktop\ForDemo";
            string thumbPath = Path.Combine(rootPath, "Thumb");
            if (Directory.Exists(thumbPath)) DirectoryDelete(thumbPath);
            Directory.CreateDirectory(thumbPath);

            int imageNumber = 0;
            foreach (string s in Directory.GetFiles(rootPath, "*.jpg"))
            {
                imageNumber++;
                Console.WriteLine("{0}:{1}", imageNumber, s);
                Image i = Image.FromFile(s);
                Image thumb = i.GetThumbnailImage(160, 120, null, IntPtr.Zero);
                thumb.Save(Path.Combine(thumbPath, GetName(imageNumber)));
            }
        }

        static void DirectoryDelete(string directoryName)
        {
            foreach (string filename in Directory.GetFiles(directoryName))
            {
                File.Delete(filename);
            }
            Directory.Delete(directoryName);
        }
        static string GetName(int imageNumber)
        {
            return String.Format("{0}.jpg", imageNumber);
        }
    }
}

Update:  A couple of folks asked me about how to do this in ASP.NET... Bertrand has a much more complete example of that here:

http://dotnetslackers.com/articles/aspnet/Generating-Image-Thumbnails-in-ASP-NET.aspx

http://weblogs.asp.net/bleroy/archive/2007/12/05/what-interpolationmode-and-compositingquality-to-use-when-generating-thumbnails-via-system-drawing.aspx

 

Published 10 July 08 09:15 by BradA
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Richard said on July 10, 2008 2:17 PM:

Due to a "feature" of GDI+, calling Image.FromFile will keep the file locked until your process exits. If you're creating thumbnails from a long-running process (ASP.NET, Windows Service, etc.), you may want to use Image.FromStream instead:

using (Stream fs = File.OpenRead(s))

using (Image i = Image.FromStream(fs))

{

   ...

}

NB: You can't dispose of the stream until you've finished with the image:

http://support.microsoft.com/kb/814675

# Omer van Kloeten said on July 10, 2008 4:12 PM:

AFAIK, running GDI+ in an ASP.NET process is considered harmful and is unsupported by Microsoft.

# Otto said on July 10, 2008 5:23 PM:

If you use this on GIF or PNG images with transparency, what happens?

# 拝啓、サカモトと申します。 said on July 10, 2008 6:43 PM:

Very Simple .NET Thumbnail Creation Code

# John S. said on July 11, 2008 1:27 AM:

GetThumbnailImage has a few issues. I highly recommend the method described here: http://www.glennjones.net/Post/799/Highqualitydynamicallyresizedimageswithnet.htm

In my opinion, the .NET image handling libraries are lacking some simple abstractions for consistent handling of jpg, gif (both transparent and not) and png resizing. It is way more complicated than it should be.

# Mike said on July 11, 2008 5:16 AM:

Also, be careful because if you don't specify an image encodig, it will just create a png. You might not notice, because when you open the file (test.jpg) it will display most of the time, but it could be an actual png file, and in some programs it might not open.

Seriously, posting these "this is simple, do it in 3 lines of code" articles are a bad idea hmmkay. Programming is not simple, and image processing is not simple either.

# Eric said on July 11, 2008 8:16 AM:

And why not use Directory.Delete(path, true) for recursive deletion of everything in the directory instead of coding a subroutine to do that?

# mihailik said on July 11, 2008 11:12 AM:

I can't believe you've forgotten to call Dispose!

# Nathanael Jones said on August 9, 2008 9:25 PM:

You can generate resized thumbnail versions on-the-fly with this module:

http://nathanaeljones.com/products/asp-net-image-resizer/

It's very stable, and includes disk caching for scalability.

You would just add ?thumbnail=jpg&maxwidth=160&maxheight=120 to the image path.

Leave a Comment

(required) 
(optional)
(required) 

Search

Go

This Blog

Syndication

Page view tracker