Click here to Skip to main content
Click here to Skip to main content

Thumbnails viewer and image processing using GDI+ and MFC

By , 24 Sep 2003
 

Introduction

I want to propose an easy way to implement an image thumbnail viewer with some image processing functions incorporated using GDI+ in a regular MFC VC++ 6.0 application.

Background

The idea is to use only what is available in a standard Microsoft Windows environment and not using one of the many custom image libraries available.

Using the code

"ImageTool" application has an image viewer that displays thumbnail images available in a folder. This view window CImageToolView uses a CImageList object and is inherited from CListView class and contains the thread that loads the thumbnails. Also has a CFoldersDlg object used for browsing computer specific folders and another object CPreviewDlg used for previewing the selected image including the result on various image operations. Both objects are inherited from CDialog class.

After starting the application we have to select a folder and in the main view we will see thumbnail images if there are images available in that folder. Selecting an image from the thumbnail images list we can see the original into the preview dialog. By applying one of the image processing operations we can see the result also into the preview dialog. The basic image processing operations are: - Mirror, - Flip, - Rotate Right 90, - Rotate Left 90, - Grayscale, - Negative, - Pseudo Colors... (gamma value), - Pseudo Threshold... (transparency range), - Lighten, - Darken, - Contrast, - Sharpen. These operations are implemented into a separate thread found into the CImageToolDoc class. We have also "Undo/Redo" capabilities with the help of Bitmap::Clone( . ) function.

The images possible to be displayed are given by the graphics file formats supported by GDI+: - BMP (Windows bitmap), - EMF (Enhanced Metafile), - Exif (Exchangeable Image File), - GIF (Graphics Interchange Format), - Icon, - Indicates the JPEG (Joint Photographic Experts Group), - PNG (Portable Network Graphics), - TIFF (Tag Image File Format), - WMF (Windows Metafile), basically everything that can be loaded into a GDI+ Bitmap object.

Anyway we have to check always if we are dealing with a GDI+ valid image. This is very easy using Bitmap::GetFlags() function like:

////
BOOL CImageToolDoc::IsImageGDIPLUSValid( CString filePath )
{
  Bitmap image( filePath.AllocSysString() );
  if( image.GetFlags() == ImageFlagsNone ) return FALSE;
  else return TRUE;
}
/////

where ImageFlagsNone, part of the ImageFlags enumeration, means that no format information is available.

The point is that after selecting a folder (CImageToolDoc::SelectDirectory( . ) ) to load into the files' list (m_vFileName) to be displayed only the supported images including the image files having an altered extension. The RunLoadThumbnailThread thread loads the image files from this list using GDI+ Bitmap object.

The code here is not complicated but I just want to note that I don't use Image::GetThumbnailImage( . ) function to obtain the thumbnail image, instead I prefer to scale the bitmap myself. I am doing this not because I cannot use:

Bitmap *pThumbnail = (Bitmap*)image.GetThumbnailImage( nDestWidth, 
    nDestHeight, NULL, NULL );

but because I cannot deal directly with the background color how I want so anyway I have to use a GDI+ Graphics object created from the image like this:

Graphics *grPhoto = Graphics::FromImage( bmPhoto );
Color colorW(255, 255, 255, 255);
Clear( colorW );

And also I don’t want to deal with the embedded thumbnail image - if the file has one - because in this case GetThumbnailImage( . ) retrieves the embedded thumbnail image and doesn't create a thumbnail image by scaling the main image to the specified size.

On the end of the thread I have to fill the image list with MFC CBitmap objects obtained from the already scaled GDI+ Bitmap objects.

The second thread - the image processing thread - uses either direct GDI+ Bitmap image processing function (e.g. RotateFlip( . ) ) or a GDI+ Graphics object created from the image, associated with a GDI+ ColorMatrix structure and a GDI+ ImageAttributes object.

There are operations that are using hard-coded parameters and others - like gamma value and transparency range - that have an associated dialog to specify the parameters. This is just for demonstration purpose and can be improved further.

I have also a "Print Preview" capability provided by the CImageToolView::OnDraw( . ) but the "Print" function needs to be improved.

The CFoldersDlg object uses a CTreeCtrl derived control (CFoldersTreeCtrl) to facilitate the folder selection and CPreviewDlg object uses a CStatic object to display the image. In CPreviewDlg I use a memory device context object to eliminate flickering.

Conclusion

Please remember that this is only for demonstration purpose and to be used like a professional application has to be improved especially on the design level regarding validating and loading image files in a multithreading operation. Also "Save" and other functions should be added. Regards to Moah whose article "Thumbnails Viewer using ListCtrl" reminded me to write this paper. Good luck!

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

Liviu Birjega
Software Developer (Senior) IBI Group
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionvote of 5++membervaibhavbvp15 Nov '12 - 18:17 
AnswerRe: vote of 5++memberkrislyy27 Nov '12 - 17:05 
GeneralRe: vote of 5++membervaibhavbvp30 Nov '12 - 15:06 
GeneralRe: vote of 5++membervaibhavbvp30 Nov '12 - 15:06 
QuestionCannot compile with VS 2010memberMichael B Pliam8 Jul '12 - 9:32 
GeneralMy vote of 5membermanoj kumar choubey16 Feb '12 - 23:55 
QuestionThread safety OK?membergordon881 Sep '11 - 19:45 
Questionhow to resize jpeg file?membertiplip30 Jul '10 - 17:16 
AnswerRe: how to resize jpeg file?membergordon883 Sep '11 - 1:20 
GeneralPrinting IssuememberBibo197829 Oct '09 - 1:24 
GeneralReally GoodmemberMANISH RASTOGI26 May '09 - 23:51 
QuestionZoom in/Zoom out...a fine examplememberNupurK8 May '07 - 2:59 
Generalgreatmemberlsmart29 Jan '07 - 15:44 
QuestionZoom in/ Out displayed image?memberRunningThread11 Dec '06 - 22:17 
GeneralPrinting ~ 100 x times too big and doesnt fitmemberBill Gates Antimatter Particle10 Nov '06 - 0:28 
GeneralComplex problem in GDI+!membermostafa_pasha15 Sep '06 - 3:00 
Questionbmp imagesmembertahinn3 Jul '06 - 1:25 
GeneralResizing the images in thumbnail at runtime..memberMehta Jigar7 Apr '06 - 10:45 
QuestionMemory leak..?memberJörgen Sigvardsson20 Feb '06 - 8:23 
Questionsave thumbnail to diskmemberLotlikar19 Nov '05 - 20:27 
GeneralSave problemmemberkzf_id6 Nov '05 - 0:33 
Generaljpeg onto screensussAnonymous10 Jun '05 - 12:34 
GeneralRe: jpeg onto screensussAnonymous1 Aug '05 - 6:15 
GeneralNull Pointermember089890888 Jun '05 - 21:17 
GeneralProblem in Printing!memberajalilqarshi31 May '05 - 20:05 
Generalquestion about CMemDCmemberFlyLogin27 May '05 - 19:02 
GeneralRe: question about CMemDCmemberGhasrfakhri10 Jun '05 - 15:18 
GeneralRe: question about CMemDCmemberFlyLogin10 Jun '05 - 18:15 
GeneralA sharpen problemmemberYuval Gross21 Mar '05 - 3:08 
GeneralcommentsmemberRiceking20 Mar '05 - 9:30 
Generalproblem compiling with visual studio.netmembermoeed2220 Jan '05 - 11:32 
GeneralA small problemmemberAlex Evans13 Jan '05 - 20:45 
GeneralGreat article !membervpas4 Jan '05 - 18:07 
GeneralPrint Preview and Printmemberprincefaisal9915 Dec '04 - 21:05 
GeneralGreat articlememberandang14 Dec '04 - 14:18 
QuestionHow to convert CBitmap to GDI+ BitmapsusssanMartinz16 Sep '04 - 14:51 
AnswerRe: How to convert CBitmap to GDI+ BitmapmemberThomas Jongepier21 Sep '04 - 10:45 
QuestionHow to save GDI+ bmp file as PDF File Format.susspubba13 Jul '04 - 19:58 
Generali need some help plzmembertahirmehmood21 Apr '04 - 22:06 
Generalwork with paletised images in GDI+memberming6915 Apr '04 - 17:49 
GeneralNice viewer too bad it crashes after 1 secmemberBRIAN BIRD24 Nov '03 - 14:36 
GeneralRe: Nice viewer too bad it crashes after 1 secmemberLiviu Birjega25 Nov '03 - 4:26 
GeneralNice sample(wiith Thumbnail Viewer)membersealchan4 Nov '03 - 20:39 
GeneralRe: Await your "Save" function!memberLiviu Birjega29 Sep '03 - 7:01 
GeneralNice to see...memberhey_wey25 Sep '03 - 15:12 
GeneralRe: Nice to see...memberLiviu Birjega25 Sep '03 - 18:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 25 Sep 2003
Article Copyright 2003 by Liviu Birjega
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid