Click here to Skip to main content
14,979,401 members
Articles / Desktop Programming / MFC
Posted 24 Sep 2003


231 bookmarked

Thumbnails viewer and image processing using GDI+ and MFC

Rate me:
Please Sign up or sign in to vote.
4.93/5 (78 votes)
24 Sep 20034 min read
Using GDI+ and MFC to create a thumbnail image viewer and some processing functions

Image 1


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.


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.


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!


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
No Biography provided

Comments and Discussions

QuestionCan I debug this project in Visual Studio 2017 Community? Pin
Member 134408062-Oct-17 5:43
MemberMember 134408062-Oct-17 5:43 
Questionvote of 5++ Pin
vaibhavbvp15-Nov-12 18:17
Membervaibhavbvp15-Nov-12 18:17 
AnswerRe: vote of 5++ Pin
krislyy27-Nov-12 17:05
Memberkrislyy27-Nov-12 17:05 
GeneralRe: vote of 5++ Pin
vaibhavbvp30-Nov-12 15:06
Membervaibhavbvp30-Nov-12 15:06 
GeneralRe: vote of 5++ Pin
vaibhavbvp30-Nov-12 15:06
Membervaibhavbvp30-Nov-12 15:06 
QuestionCannot compile with VS 2010 Pin
Michael B Pliam8-Jul-12 9:32
MemberMichael B Pliam8-Jul-12 9:32 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey16-Feb-12 23:55
professionalManoj Kumar Choubey16-Feb-12 23:55 
QuestionThread safety OK? Pin
gordon881-Sep-11 19:45
professionalgordon881-Sep-11 19:45 
Questionhow to resize jpeg file? Pin
tiplip30-Jul-10 17:16
Membertiplip30-Jul-10 17:16 
AnswerRe: how to resize jpeg file? Pin
gordon883-Sep-11 1:20
professionalgordon883-Sep-11 1:20 
GeneralPrinting Issue Pin
Bibo197829-Oct-09 1:24
MemberBibo197829-Oct-09 1:24 
GeneralReally Good Pin
MANISH RASTOGI26-May-09 23:51
MemberMANISH RASTOGI26-May-09 23:51 
QuestionZoom in/Zoom out...a fine example Pin
NupurK8-May-07 2:59
MemberNupurK8-May-07 2:59 
Generalgreat Pin
lsmart29-Jan-07 15:44
Memberlsmart29-Jan-07 15:44 
QuestionZoom in/ Out displayed image? Pin
RunningThread11-Dec-06 22:17
MemberRunningThread11-Dec-06 22:17 
GeneralPrinting ~ 100 x times too big and doesnt fit Pin
Bill Gates Antimatter Particle10-Nov-06 0:28
MemberBill Gates Antimatter Particle10-Nov-06 0:28 
GeneralComplex problem in GDI+! Pin
mostafa_pasha15-Sep-06 3:00
Membermostafa_pasha15-Sep-06 3:00 
Questionbmp images Pin
tahinn3-Jul-06 1:25
Membertahinn3-Jul-06 1:25 
GeneralResizing the images in thumbnail at runtime.. Pin
Mehta Jigar7-Apr-06 10:45
MemberMehta Jigar7-Apr-06 10:45 
QuestionMemory leak..? Pin
Jörgen Sigvardsson20-Feb-06 8:23
MemberJörgen Sigvardsson20-Feb-06 8:23 
Questionsave thumbnail to disk Pin
Lotlikar19-Nov-05 20:27
MemberLotlikar19-Nov-05 20:27 
GeneralSave problem Pin
kzf_id6-Nov-05 0:33
Memberkzf_id6-Nov-05 0:33 
Generaljpeg onto screen Pin
Anonymous10-Jun-05 12:34
MemberAnonymous10-Jun-05 12:34 
GeneralRe: jpeg onto screen Pin
Anonymous1-Aug-05 6:15
MemberAnonymous1-Aug-05 6:15 
GeneralNull Pointer Pin
089890888-Jun-05 21:17
Member089890888-Jun-05 21:17 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.