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:
https://dotnetslackers.com/articles/aspnet/Generating-Image-Thumbnails-in-ASP-NET.aspx
Comments
Anonymous
July 10, 2008
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/814675Anonymous
July 10, 2008
AFAIK, running GDI+ in an ASP.NET process is considered harmful and is unsupported by Microsoft.Anonymous
July 10, 2008
If you use this on GIF or PNG images with transparency, what happens?Anonymous
July 10, 2008
Very Simple .NET Thumbnail Creation CodeAnonymous
July 10, 2008
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.Anonymous
July 10, 2008
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.Anonymous
July 11, 2008
And why not use Directory.Delete(path, true) for recursive deletion of everything in the directory instead of coding a subroutine to do that?Anonymous
July 11, 2008
I can't believe you've forgotten to call Dispose!Anonymous
August 09, 2008
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.