Click here to Skip to main content
13,352,624 members (35,329 online)
Click here to Skip to main content
Add your own
alternative version


200 bookmarked
Posted 14 Sep 2003

Thumbnails Viewer using ListCtrl

, 27 Jan 2006
Rate this:
Please Sign up or sign in to vote.
Show thumbnails of images, include JPG, TIFF, BMP, etc.

Sample Image - ThumbViewer.jpg


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.


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"),
  LVS_AUTOARRANGE, rect, pParentWnd, nID, pContext);

 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++)

 // remove all items from list view


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


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

 // Border Size
 RECT rcBorder;;

 const float fRatio=(float)THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;

 int XDest, YDest, nDestWidth, nDestHeight;
     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);

  CxImage image(path, nImageType);


  // Calculate Rect to fit to canvas
  const float fImgRatio=(float)image.GetHeight()/image.GetWidth();
  if(fImgRatio > fRatio)

  CClientDC cdc(pView);
  HDC hDC=::CreateCompatibleDC(cdc.m_hDC);
  HBITMAP bm = CreateCompatibleBitmap(cdc.m_hDC, THUMBNAIL_WIDTH, 
  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;
  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


 _endthreadex( 0 );

 return 0;


You can download CxImage freely from here.


  • 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.


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


About the Author

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.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 941034217-Jul-16 21:34
memberMember 941034217-Jul-16 21:34 
QuestionNot able to run this code in dialog box Pin
chavakkula16-Jul-15 5:20
memberchavakkula16-Jul-15 5:20 
GeneralMy vote of 5 Pin
43noodles2-Jul-10 23:46
member43noodles2-Jul-10 23:46 
Generalhello, I came across one question. I need your help... Pin
cciill5-Sep-09 7:42
membercciill5-Sep-09 7:42 
QuestionWhy can't I hide the VScroll in listctrl??? Pin
Sophia2925-Nov-08 22:30
memberSophia2925-Nov-08 22:30 
Well done! Poke tongue | ;-P
I want to hide VScroll and just show HScoll in listctrl, but it seems doesn't work!
Do you know why?

I want to program better and better from now on!

GeneralDatagrid thumbnail images Pin
skyair29-Jun-08 16:49
memberskyair29-Jun-08 16:49 
GeneralHai Pin
Sivan Manimala3-Jun-08 0:09
memberSivan Manimala3-Jun-08 0:09 
QuestionPossible to only highlight name and not picture? Pin
alan9329-May-08 11:09
memberalan9329-May-08 11:09 
AnswerRe: Possible to only highlight name and not picture? Pin
DevoraNur24-Oct-09 21:51
memberDevoraNur24-Oct-09 21:51 
GeneralRe: Possible to only highlight name and not picture? Pin
alan9328-Oct-09 3:44
memberalan9328-Oct-09 3:44 
General안녕하세요~ [modified] Pin
rider7617-Mar-08 16:22
memberrider7617-Mar-08 16:22 
Questionhow can do it Pin
thrudoor28-Nov-07 0:27
memberthrudoor28-Nov-07 0:27 
GeneralMore smooth Picture! Pin
OpenGL_VC5-Apr-07 21:40
memberOpenGL_VC5-Apr-07 21:40 
GeneralRe: More smooth Picture! Pin
moah14-May-07 21:58
membermoah14-May-07 21:58 
GeneralLINK : fatal error LNK1181: cannot open input file "./lib/cximagecrtu.lib" Pin
SKhokalay7-Dec-06 0:46
memberSKhokalay7-Dec-06 0:46 
GeneralLINK : fatal error LNK1181: cannot open input file "./lib/cximagecrtu.lib" Pin
SKhokalay7-Dec-06 0:51
memberSKhokalay7-Dec-06 0:51 
GeneralRe: LINK : fatal error LNK1181: cannot open input file "./lib/cximagecrtu.lib" Pin
cristitomi5-Apr-07 0:32
membercristitomi5-Apr-07 0:32 
GeneralRe: LINK : fatal error LNK1181: cannot open input file "./lib/cximagecrtu.lib" Pin
Son Ji Seon26-May-09 4:28
memberSon Ji Seon26-May-09 4:28 
GeneralRe: LINK : fatal error LNK1181: cannot open input file "./lib/cximagecrtu.lib" Pin
Member 112204359-Nov-14 22:07
memberMember 112204359-Nov-14 22:07 
QuestionProblems in adding a new bar. Pin
perhapszy2-Aug-06 23:18
memberperhapszy2-Aug-06 23:18 
AnswerRe: Problems in adding a new bar. Pin
moah4-Aug-06 20:20
membermoah4-Aug-06 20:20 
GeneralRe: Problems in adding a new bar. Pin
perhapszy7-Aug-06 23:40
memberperhapszy7-Aug-06 23:40 
Questionlill more help plz Pin
tahinn6-Jul-06 20:16
membertahinn6-Jul-06 20:16 
AnswerRe: lill more help plz Pin
moah8-Jul-06 8:07
membermoah8-Jul-06 8:07 
GeneralRe: lill more help plz Pin
tahinn12-Jul-06 3:50
membertahinn12-Jul-06 3:50 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 27 Jan 2006
Article Copyright 2003 by moah
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid