Click here to Skip to main content
15,892,537 members
Articles / Desktop Programming / MFC

Thumbnails Viewer using ListCtrl

Rate me:
Please Sign up or sign in to vote.
4.86/5 (73 votes)
27 Jan 2006CPOL1 min read 639.1K   19.1K   204   148
Show thumbnails of images, include JPG, TIFF, BMP, etc.

Sample Image - ThumbViewer.jpg

Introduction

There are several image libraries and sources currently available. So I made my mind up to make a free Image Viewer using a free image library, and I got many free demo programs. I think a good image viewer must be able to show thumbnail images in a selected directory. I found some sources showing thumbnails, but they didn't support several formats, only BMP files. This thumbnail viewer is based on CxImage, so if there is an image format supported by CxImage, this viewer also supports that format.

Implementation

I want to show you the core part in this article. How to make a CListCtrl, CImageList, and then how to load images and attach that image to an ImageList.

1. Creating CListCtrl and CImageList

I used a CListView instead of CListCtrl, but you know that CListView uses CListCtrl internally. So overriding Create(...) will make your icon view CListCtrl initially. And then on OnInitialUpdate(), make ImageList which can support 24 bit color images and attach to CListCtrl.

return CListView::Create(lpszClassName, _T("ListView"),
  dwStyle|LVS_SHOWSELALWAYS|LVS_ALIGNTOP|LVS_ICON|LVS_SINGLESEL|
  LVS_AUTOARRANGE, rect, pParentWnd, nID, pContext);

....
m_ImageListThumb.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1);
 ListCtrl.SetImageList(&m_ImageListThumb, LVSIL_NORMAL);

2. Load Images and Insert Items

Create a Compatible DC and a Bitmap Handle. Stretch a loaded image to the Bitmap Handle and then attach it to CBitmap. Finally, replace CBitmap with an image in the ImageList.

unsigned __stdcall CThumbViewerView::LoadThumbNail(LPVOID lpParam)
{
 CThumbViewerView* pView=(CThumbViewerView*)lpParam;
 CThumbViewerDoc* pDoc=pView->GetDocument();

 CListCtrl& ListCtrl=pView->GetListCtrl();
 CImageList* pImgList=&pView->m_ImageListThumb;

 // reset our image list
 for(int i=0; i<pImgList->GetImageCount(); i++)
  pImgList->Remove(i); 

 // remove all items from list view
 ListCtrl.DeleteAllItems();

 pImgList->SetImageCount(pDoc->m_vFileName.size());

 char path[MAX_PATH];
 vector<CString>::iterator iter;
 
 // Set redraw to FALSE to avoid flickering during adding new items
 ListCtrl.SetRedraw(FALSE);
 int nIndex=0;
 for(iter=pDoc->m_vFileName.begin(); 
     iter!=pDoc->m_vFileName.end() && pView->m_bTerminate!=true; 
     iter++, nIndex++)
 {
          ListCtrl.InsertItem(nIndex, *iter, nIndex);
 }

 ListCtrl.SetRedraw(TRUE);
 ListCtrl.Invalidate();

 // Create Brushes for Border and BackGround
 HBRUSH hBrushBorder=::CreateSolidBrush(RGB(192, 192, 192));
 HBRUSH hBrushBk=::CreateSolidBrush(RGB(255, 255, 255));

 // Border Size
 RECT rcBorder;
 rcBorder.left=rcBorder.top=0;
 rcBorder.right=THUMBNAIL_WIDTH;
 rcBorder.bottom=THUMBNAIL_HEIGHT;

 const float fRatio=(float)THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;

 int XDest, YDest, nDestWidth, nDestHeight;
 nIndex=0;
 for(iter=pDoc->m_vFileName.begin(); 
     iter!=pDoc->m_vFileName.end() && pView->m_bTerminate!=true; 
     iter++, nIndex++)
 {
  // Load Image File
  sprintf(path, "%s\\%s", pDoc->m_strCurrentDirectory, *iter);

  int nImageType=pDoc->GetTypeFromFileName(path);
  if(nImageType==CXIMAGE_FORMAT_UNKNOWN)
   continue;

  CxImage image(path, nImageType);

  if(image.IsValid()==false)
   continue;

  // Calculate Rect to fit to canvas
  const float fImgRatio=(float)image.GetHeight()/image.GetWidth();
  if(fImgRatio > fRatio)
  {
   nDestWidth=THUMBNAIL_HEIGHT/fImgRatio;
   XDest=(THUMBNAIL_WIDTH-nDestWidth)/2;
   YDest=0;
   nDestHeight=THUMBNAIL_HEIGHT;
  }
  else
  {
   XDest=0;
   nDestWidth=THUMBNAIL_WIDTH;
   nDestHeight=THUMBNAIL_WIDTH*fImgRatio;
   YDest=(THUMBNAIL_HEIGHT-nDestHeight)/2;
  }

  CClientDC cdc(pView);
  HDC hDC=::CreateCompatibleDC(cdc.m_hDC);
  HBITMAP bm = CreateCompatibleBitmap(cdc.m_hDC, THUMBNAIL_WIDTH, 
                                      THUMBNAIL_HEIGHT);
  HBITMAP pOldBitmapImage = (HBITMAP)SelectObject(hDC,bm);
  // Draw Background
  ::FillRect(hDC, &rcBorder, hBrushBk);

  // Draw Image
  image.Stretch(hDC, XDest, YDest, nDestWidth, nDestHeight);

  // Draw Border
  ::FrameRect(hDC, &rcBorder, hBrushBorder);

  SelectObject(hDC, pOldBitmapImage);

  // Attach to Bitmap and Replace image in CImageList
  CBitmap bitmap;
  bitmap.Attach(bm);
  pImgList->Replace(nIndex, &bitmap, NULL);

  // Redraw only a current item for removing flickering and fast speed.
  ListCtrl.RedrawItems(nIndex, nIndex);

  // Release used DC and Object
  DeleteDC(hDC);
  DeleteObject(bm);
 }
 DeleteObject(hBrushBorder);
 DeleteObject(hBrushBk);

 ListCtrl.Invalidate();
 pView->m_bRunning=false;
 pView->m_bTerminate=false;

 _endthreadex( 0 );

 return 0;
}

Acknowledgment

You can download CxImage freely from here.

History

  • Ver. 1.0  15, Sep. 2003
    • First release.
  • Ver. 1.1  24, Mar. 2005
    • Fixed crashing when changing a directory fast.
    • Supports Unicode option using preprocessor _UNICODE.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Korea (Republic of) Korea (Republic of)
Youngjin Kim lives in South Korea. I'm interested in every part of Computer Science, cause it has not been long time since graduate a University.
But now I'm working and researching on Pattern Recognition. Using that trying to recognize a Handwriting Prints. Korean and Chinese are my interesting Research Part.

Comments and Discussions

 
QuestionProblems in adding a new bar. Pin
perhapszy2-Aug-06 22:18
perhapszy2-Aug-06 22:18 
AnswerRe: Problems in adding a new bar. Pin
moah4-Aug-06 19:20
moah4-Aug-06 19:20 
GeneralRe: Problems in adding a new bar. Pin
perhapszy7-Aug-06 22:40
perhapszy7-Aug-06 22:40 
Questionlill more help plz Pin
tahinn6-Jul-06 19:16
tahinn6-Jul-06 19:16 
AnswerRe: lill more help plz Pin
moah8-Jul-06 7:07
moah8-Jul-06 7:07 
GeneralRe: lill more help plz Pin
tahinn12-Jul-06 2:50
tahinn12-Jul-06 2:50 
QuestionI can't build with Visual C++ 8 (2005) Pin
Anderson Luís5-Jul-06 17:34
Anderson Luís5-Jul-06 17:34 
AnswerRe: I can't build with Visual C++ 8 (2005) Pin
moah8-Jul-06 6:53
moah8-Jul-06 6:53 
The errors are occured from the 3rd party library, CSizingControlBar.
And unfortunately it was published a little long time ago. Frown | :(

You can infer that the problems are because of mismatching return type of two functions.
Therefore you can simply fix the problem. Smile | :)

1. Open scbarg.h, and see line 103.
Change afx_msg UINT OnNcHitTest(CPoint point);
to afx_msg LRESULT OnNcHitTest(CPoint point);

2. Open scbarg.cpp and see line 145
Change UINT CSizingControlBarG::OnNcHitTest(CPoint point)
to LRESULT CSizingControlBarG::OnNcHitTest(CPoint point)

3. Open sizecbar.h and see line 167
Change afx_msg UINT OnNcHitTest(CPoint point);
to afx_msg LRESULT OnNcHitTest(CPoint point);

4. Open sizecbar.cpp and see line 573
Change UINT CSizingControlBar::OnNcHitTest(CPoint point)
to LRESULT CSizingControlBar::OnNcHitTest(CPoint point)

Now, you'll see no more error. Smile | :)
GeneralRe: I can't build with Visual C++ 8 (2005) Pin
tdjdyq23-Jul-08 19:26
tdjdyq23-Jul-08 19:26 
GeneralRe: I can't build with Visual C++ 8 (2005) Pin
wissli11-Jan-10 23:00
wissli11-Jan-10 23:00 
Questionshow only .bmp images Pin
tahinn4-Jul-06 0:41
tahinn4-Jul-06 0:41 
AnswerRe: show only .bmp images Pin
moah4-Jul-06 18:46
moah4-Jul-06 18:46 
GeneralRe: show only .bmp images Pin
tahinn5-Jul-06 3:17
tahinn5-Jul-06 3:17 
QuestionCFormView Pin
Karl Bahr29-Jun-06 10:05
Karl Bahr29-Jun-06 10:05 
AnswerRe: CFormView Pin
moah29-Jun-06 14:50
moah29-Jun-06 14:50 
GeneralRe: CFormView [modified] Pin
Rene Ortner4-Jul-06 6:27
Rene Ortner4-Jul-06 6:27 
GeneralRe: CFormView Pin
moah4-Jul-06 18:42
moah4-Jul-06 18:42 
GeneralTwo or more thumbnails Pin
alwittta8-Mar-06 0:47
alwittta8-Mar-06 0:47 
GeneralRe: Two or more thumbnails Pin
moah8-Mar-06 22:18
moah8-Mar-06 22:18 
GeneralRe: Two or more thumbnails Pin
alwittta8-Mar-06 23:27
alwittta8-Mar-06 23:27 
GeneralRe: Two or more thumbnails Pin
moah9-Mar-06 0:31
moah9-Mar-06 0:31 
GeneralRe: Two or more thumbnails Pin
alwittta9-Mar-06 0:53
alwittta9-Mar-06 0:53 
GeneralRe: Two or more thumbnails Pin
moah10-Mar-06 1:03
moah10-Mar-06 1:03 
Questionsome information about the image Pin
ali2121212121212121-Feb-06 4:26
ali2121212121212121-Feb-06 4:26 
AnswerRe: some information about the image Pin
moah21-Feb-06 14:54
moah21-Feb-06 14:54 

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.