Click here to Skip to main content
6,295,667 members and growing! (14,336 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

A C# based thumbnail viewer

By Sreejai R. Kurup

C# based thumbnail viewer employing a BackgroundWorker to load images in the background.
C# 2.0, Windows, .NET 1.1, .NET 2.0VS.NET2003, VS2005, Dev
Posted:24 Feb 2007
Updated:19 May 2007
Views:29,572
Bookmarked:30 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
14 votes for this article.
Popularity: 3.81 Rating: 3.32 out of 5
3 votes, 21.4%
1
2 votes, 14.3%
2
1 vote, 7.1%
3
1 vote, 7.1%
4
7 votes, 50.0%
5
Title:       A C# thumbnail control using background worker
Author:      Sreejai R. Kurup 
Email:       sreejai@hotmail.com
Environment: C# .NET 2.0
Keywords:    Control, Imaging, Thumbnail, Background worker
Level:       Intermediate
Description: A C# based thumbnail viewer derived from the ListView control
Section      Miscellaneous
SubSection   General

JThumbnailView Demo project

Introduction

Many a times we come across situations where it is required to show images from a directory as thumbnails. This control JThumnailView exactly does that. The additional feature of this control is that it uses a background worker to load the images asynchronously.

Background

It all started when I wanted to send some photos back home electronically. Of course, you could do that using e-mails, however, when doing so mostly it would be zipped and sent. When I saw my parents struggling to keep track of where they saved the photos for later view, I realised that I needed to provide them something which could be controlled by me. Hence, I started developing an application which once installed on a machine will serve as an automatic picture downloader and a basic viewer. Thus, I was faced with the following challenges:

  1. How to do an automatic download? My answer came in the form of the edtFTPnet component, with which I could go and download file(s) from an FTP server. However, this posed another problem, everytime I want to send new photos, I needed some mechanism which would transparently do it without any manual intervention. That is when I decided to use an XML file which the application would read and perform the necessary action. However, once I started developing the XML file, I wanted to encrypt it and give a different extension. Hence, I used a DESCryptoServiceProvider to perform the encryption/decrytion for the file. Now, all I have to do is send my file (which has a *.pfi) extension and give a password. When this file is double clicked, it will ask for a password and it will start downloading the photos automatically from my FTP site.
  2. Now the second challenge for me to provide a simple photo viewer in the same application so that the user need not worry about where the photos are stored. For this, I needed two components, one Explorer-like folder viewer and a thumbnail viewer. That is when I got the WindowsExplorer component written by Rajesh Lal. Though I had to make little modifications to suit my requirements, it saved me a lot of time.
  3. The only thing remaining was a thumbnail viewer. I searched for a good one, but most of them were for ASP.NET. Finally I saw one, but again it was based on VC++. That is when I decided to write one. I made it a control so that people can re-use it. This article describes the JThumnailView component I created for this purpose. For those who want the full version of the application, please contact me by email. I will be more than happy to provide it (though it is still in the beta phase!!!).

Using the code

The component is very simple to understand. It has been derived from the standard ListView control so that I need not worry about things like scrolling, sorting etc. :).

The following are the main published properties:

public int ThumbNailSize; //default value: 95
By default, it has a value of 95 (Windows explorer seemed to use this size, so I made it as default)
public int ThumbBorderColor; //default value: Color.Wheat

Set this if you want the thumbnails to have a different border.
public string FolderName; //default value: Application folder
This is the directory from which the thumbnails are to be loaded. The component has another property CanLoad which should be set to true to load the images. This should be set to true on the constructor of the form.

One of the main problems with most of the thumbnail viewers is that if the number of files in a directory are high, then the viewer takes a lot of time to load. To avoid this, the technique I am using is to create a Default thumbnail which is just a square of the thumbnail size and initially set this as the imageindex for all the items. This will give the user an impression that the thumbnails have been loaded. Now the actual thumbnails are loaded in the background using a BackgroundWorker.
private BackgroundWorker myWorker = new BackgroundWorker();
The loading of the items is handled in the method: ReloadItems()

private void ReLoadItems() 
{ 
    BeginUpdate(); 
    Items.Clear(); 
    LargeImageList.Images.Clear(); 
    AddDefaultThumb(); 
    
    string strFilter = "*.jpg;*.png;*.gif;*.bmp"; 
    List fileList = new List(); 
    string[] arExtensions = strFilter.Split(';'); 
    
    foreach (string filter in arExtensions) 
    { 
        string[] strFiles = Directory.GetFiles(folderName, filter); 
        fileList.AddRange(strFiles); 
        for (int i = 0; i < strFiles.Length; i++) 
        { 
            FileInfo fiTemp = new FileInfo(strFiles[i]); 
            Items.Add(fiTemp.Name, 0).Tag = strFiles[i]; } 
        } 
        
        fileList.Sort(); 
        EndUpdate(); 
        if (myWorker != null) { myWorker.RunWorkerAsync(fileList); } 
    }
}
The AddDefaultThumb() method creates a default thumbnail as mentioned above. The actual thumbnails are drawn in the DoWork() event of the BackgroundWorker. The thumbnails are drawn using the usual graphic methods.

Points of Interest

Definitely the use of the BackgroundWorker improved the efficiency of the viewer considerably. Another interesting thing was the use of the PixelOffSetMode and InterpolationMode of the Graphics object. When I drew the thumbnails first, I could not match the clarity of the images in the Windows Explorer thumbnail. That is when I tried out the various PixelOffSetModes and the InterpolationModes. I achieved the desired result with the following combination:

    PixelOffsetMode = PixelOffsetMode.None;
   
    InterpolationMode = InterpolationMode.HighQualityBicubic;  
    

History

Version 1.1

Thanks to all the valuable comments, I have modified the component to include some more checks. :). I have also modified the demo to include a minimal picture viewer. Thanks once again to all of us especially Michael for his special interest and feedback. I still have not really tested dynamic dispose and creation and hence not sure whether the problem is still there.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sreejai R. Kurup


Member

Occupation: Web Developer
Location: United Arab Emirates United Arab Emirates

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralAbout the source code PinmemberCisily0:30 13 May '09  
GeneralStill inconsistency with the thumbnail and the filename Pinmembermartinmoesbypetersen1:24 10 Jan '09  
GeneralThumbnail Generation PinmemberMember 352586323:09 17 Dec '08  
GeneralMy vote of 1 PinmemberWertugo55522:53 8 Dec '08  
GeneralGreat Work!! PinmemberRichard Blythe20:11 10 Oct '08  
Generalfull version of the application PinmemberMember 368254820:55 3 Aug '08  
GeneralError Pinmembermfmanca10:57 30 Oct '07  
GeneralRe: Error PinmemberEdward Ceballos23:41 29 May '08  
Generalgetting filename of selected items PinmemberElsys19:05 19 Oct '07  
GeneralStop BackGroundWorker Pinmemberfracasado8:56 31 Aug '07  
GeneralRe: Stop BackGroundWorker PinmemberSreejai R. Kurup19:30 31 Aug '07  
Generalcan this be modded to work for mpeg thumbnails ? PinmemberUltraWhack8:09 31 May '07  
GeneralRe: can this be modded to work for mpeg thumbnails ? PinmemberSreejai R. Kurup19:03 31 May '07  
GeneralRe: can this be modded to work for mpeg thumbnails ? PinmemberUltraWhack3:22 1 Jun '07  
GeneralBug: Thumbnail image inconsistency with real image Pinmembermpgjunky7:06 17 May '07  
GeneralCrashes if you make it start loading new thumbnails before it's finished Pinmembermrhaan8:40 11 Apr '07  
GeneralBackgroundWorker PinmemberBushRob13:11 27 Mar '07  
GeneralRe: BackgroundWorker PinmemberSreejai R. Kurup20:48 4 Apr '07  
GeneralRe: BackgroundWorker Pinmemberwmhp113:15 20 Apr '07  
AnswerRe: BackgroundWorker [modified] Pinmembermpgjunky7:18 17 May '07  
GeneralNo source code Pinmembercykophysh3922:53 24 Feb '07  
GeneralRe: No source code PinmemberSreejai R. Kurup1:01 25 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 May 2007
Editor:
Copyright 2007 by Sreejai R. Kurup
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project