Creating Thumbnail Images with C#

The other day I needed to throw bunch of images online and arrange them in a simple HTML based gallery. I didn’t need anything fancy – not light box effects, no formatting, etc… All that stuff was already in place, I just needed to make bunch of thumbnails and generate very basic HTML to arrange the images into columns using a table. Doing this by hand seemed very tedious so I decided to look for a tool that would do this for me.

Turns out there is about a billion different tools that do exactly that – and roughly half of them are spyware ridden death traps. I found a dozen scripts that would actually do this for you in real time on the server. I found scripts, windows tools for the command line, nice GUI applications with wide range of settings you could choose from. All kinds of apps – but I didn’t like any of them, and I didn’t feel like spending several hours looking for just the right one. Not if I could spend a few hours creating one.

The end result was the Simple Gallery Maker:

Simple Gallery Maker

How does it work? You give it path to a folder, choose thumbnail size, number of columns and it will iterate through all the files, generate thumbnails and spit out simple HTML page. It’s not very smart, it does not really test if the files in the folder are actual images – it just tries every single one, and simply skips files that it can’t resize. Oh, and it also will skip everything with a “tmb_” prefix to avoid resizing thumbnails it just generated. But it is enough for my needs.

Now, the interesting part is how exactly does it generate the thumbnails. When I sat down to create this tool, I expected a little bit of work, but it turns out that .NET has everything you need built into the System.Drawing libraries. Here is a simplified version of the code I used to generate the thumbnails:

string path = "/path/to/the/image";
string output = "/where/to/save/thumbnail"
int width = 100;
int height = 100;
 
// create an image object
Image image = System.Drawing.Image.FromFile(path);
 
// create a thumbnail (ignore ThumbnailCallback for now)
Image thumbnail = image.GetThumbnailImage(width, height, 
        new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
 
// save the thumbnail to disk
thumbnail.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);

The GetThumbnailImage method expects you to pass in a pointer to a function that is to be called when the conversion fails. Unfortunately, there is no way to skip it so you will need to include it it somewhere in your class. If you don’t want to do anything fancy, this will usually do:

 public bool ThumbnailCallback()
{
      return true;
}

That’s it. That’s how you make thumbnails with C#. I’m actually quite amazed that all it took was a few lines of code

This entry was posted in programming and tagged . Bookmark the permalink.



4 Responses to Creating Thumbnail Images with C#

  1. Zel FRANCE Mozilla Firefox Windows Terminalist says:

    I use Irfanview for my image conversion needs and it works fairly well. Batch conversion and renaming are there, and you can select entire folders (with or without subdirectories) or individual files. It’s also fairly light, and doesn’t seem to include ad or spyware. I really couldn’t tell you much more about it since I rarely need to use it. As far as I know, it doesn’t generate the HTML for a gallery but you could probably write a Python script to do it in less time than I took to type this message.

    As for the magic of the .NET framework… Good thing your images weren’t in some less popular format, or you would have been in a bit more trouble. I tested the app and the resizing part could be improved upon, for one it doesn’t maintain the aspect ratio of the original. If you need that all images have the specified width/height, you could add some white/black padding on the sides, or convert to .png and use transparency.

    Reply  |  Quote
  2. Luke Maciak UNITED STATES Mozilla Firefox Linux Terminalist says:

    @ Zel:

    Ah, yes – good old IfranView. I haven’t used it in so long I completely forgot it existed. :)

    There are lots of tools that do the resizing song and dance. There used to be a XP Powertoy that allowed you to do batch resize from the context menu. Also, the Microsoft Office Picture Manager tool does this job fairly well and surprisingly has a fairly good ratio of features to simplicity of use.

    As for the aspect ratio – yeah, that was kinda by design. I opted for squashed images versus un-eaven gallery. I like your suggestion of adding padding though sometimes you would need to crop rather than pad. Looks like an interesting problem though.

    Reply  |  Quote
  3. MrJones GERMANY Mozilla Firefox Windows says:

    hey,
    i was just looking for your all in one super ultimate god mode windows program (you should name it like that ;) ) but i couldnt find it neither in project nor anywhereelse.

    Where did you hide it?
    Greetings

    Reply  |  Quote
  4. Luke Maciak UNITED STATES Mozilla Firefox Linux Terminalist says:

    @ MrJones:

    Oh, yeah… I should add a link to it on the sidebar somewhere. :P

    Here it is:
    http://sites.maciak.net/setup-assistant

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>