As part of a project to manage my collection of photos, I wanted to add the capability of displaying a series of files as thumbnails. This posed me with three problems:
- how to preserve the aspect ratio of the original images
- how to generate the thumbnails without tying up the application
- how to avoid running out of memory
Because generating thumbnails of images is (I think) of reasonably wide interest I thought I'd write it up as a code snippet. To recreate the project, start with a blank form and add two buttons, btnDetails and btnThumbs. Add a BackgroundWorker control named bgwThumbs. Add a ListView control and dock it to the bottom of the form. Add one column (Name) to the Details display.
The only thing you should have to change in the code is the value of the global variable, Path. Set this to the fully qualified path of a folder containing image files. The variable, Filter, is currently set to ignore all files except jpg, png and gif.
When you run the application you will see the list of files in the specified folder presented in Details view. Click on Thumbs to see the thumbnails. If you have a lot of files you will see the thumbnails being displayed as they are generated by the background thread. Because the generation is being done in the background you can switch between views at will. The thumbnail generation will not tie up the GUI.
Further comments on the code appear inline. Please feel free to comment and suggest improvements.