Click here to Skip to main content
15,867,594 members
Articles / Desktop Programming / MFC
Article

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 371.2K   24.8K   232   47
Using GDI+ and MFC to create a thumbnail image viewer and some processing functions

Image 1

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


Written By
Software Developer (Senior) IBI Group
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPrinting Issue Pin
Bibo197829-Oct-09 1:24
Bibo197829-Oct-09 1:24 
GeneralReally Good Pin
MANISH RASTOGI26-May-09 23:51
MANISH RASTOGI26-May-09 23:51 
QuestionZoom in/Zoom out...a fine example Pin
NupurK8-May-07 2:59
NupurK8-May-07 2:59 
Generalgreat Pin
lsmart29-Jan-07 15:44
lsmart29-Jan-07 15:44 
QuestionZoom in/ Out displayed image? Pin
RunningThread11-Dec-06 22:17
RunningThread11-Dec-06 22:17 
GeneralPrinting ~ 100 x times too big and doesnt fit Pin
Bill Gates Antimatter Particle10-Nov-06 0:28
Bill Gates Antimatter Particle10-Nov-06 0:28 
GeneralComplex problem in GDI+! Pin
mostafa_pasha15-Sep-06 3:00
mostafa_pasha15-Sep-06 3:00 
Questionbmp images Pin
tahinn3-Jul-06 1:25
tahinn3-Jul-06 1:25 
GeneralResizing the images in thumbnail at runtime.. Pin
Mehta Jigar7-Apr-06 10:45
Mehta Jigar7-Apr-06 10:45 
QuestionMemory leak..? Pin
Jörgen Sigvardsson20-Feb-06 8:23
Jörgen Sigvardsson20-Feb-06 8:23 
Questionsave thumbnail to disk Pin
Lotlikar19-Nov-05 20:27
Lotlikar19-Nov-05 20:27 
GeneralSave problem Pin
kzf_id6-Nov-05 0:33
kzf_id6-Nov-05 0:33 
Generaljpeg onto screen Pin
Anonymous10-Jun-05 12:34
Anonymous10-Jun-05 12:34 
GeneralRe: jpeg onto screen Pin
Anonymous1-Aug-05 6:15
Anonymous1-Aug-05 6:15 
GeneralNull Pointer Pin
089890888-Jun-05 21:17
089890888-Jun-05 21:17 
GeneralProblem in Printing! Pin
ajalilqarshi31-May-05 20:05
ajalilqarshi31-May-05 20:05 
Generalquestion about CMemDC Pin
FlyLogin27-May-05 19:02
FlyLogin27-May-05 19:02 
GeneralRe: question about CMemDC Pin
Ghasrfakhri10-Jun-05 15:18
Ghasrfakhri10-Jun-05 15:18 
GeneralRe: question about CMemDC Pin
FlyLogin10-Jun-05 18:15
FlyLogin10-Jun-05 18:15 
GeneralA sharpen problem Pin
Member 95404421-Mar-05 3:08
Member 95404421-Mar-05 3:08 
Generalcomments Pin
Riceking20-Mar-05 9:30
Riceking20-Mar-05 9:30 
Generalproblem compiling with visual studio.net Pin
moeed2220-Jan-05 11:32
moeed2220-Jan-05 11:32 
GeneralA small problem Pin
Alex Evans13-Jan-05 20:45
Alex Evans13-Jan-05 20:45 
GeneralGreat article ! Pin
vpas4-Jan-05 18:07
vpas4-Jan-05 18:07 
GeneralPrint Preview and Print Pin
princefaisal9915-Dec-04 21:05
princefaisal9915-Dec-04 21:05 

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.