![]() |
Desktop Development »
Miscellaneous »
General
Intermediate
A C# based thumbnail viewerBy Sreejai R. KurupC# 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
|
||||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
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
![]()
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.
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:
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!!!). 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: 95By 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
public string FolderName; //default value: Application folderThis 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. 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.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;
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.
General
News
Question
Answer
Joke
Rant
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 |