Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Thumbnails viewer and image processing using GDI+ and MFC

0.00/5 (No votes)
24 Sep 2003 2  
Using GDI+ and MFC to create a thumbnail image viewer and some processing functions

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