We know that you can make by CImageList listctr show pictures, but with the addition of image size and image size must be consistent CImageList created to appear. Recently met a demand, we need to put a lot of sizes of jpeg images to display thumbnails of the list box.
First create CImageList specified size
1 void CListEx::setCreateImageList(int iWidth, int iheight) 2 { 3 m_iImageWidth = iWidth; 4 m_iImageHeight = iheight; 5 m_imageList.Create(m_iImageWidth, m_iImageHeight, ILC_COLOR32, 20, 1); 6 SetImageList(&m_imageList, LVSIL_SMALL); 7 }
Using Gdi to scale, the path is the path of the picture, jpeg supports, no conversion format
void CListEx::AddImage(LPCSTR imagePath) { WCHAR path[512] = { 0 }; ::MultiByteToWideChar(CP_ACP, 0, (const char *)imagePath, strlen(imagePath), path, sizeof(path)); Gdiplus::Bitmap bmp(path); int sourceWidth = m_iImageWidth; int sourceHeight = bmp.GetHeight(); if (sourceHeight > m_iImageHeight) { sourceHeight = m_iImageHeight; } else { sourceHeight =bmp.GetHeight (); } // set the size of thumbnails Gdiplus pThumbnail :: = * Bitmap (Bitmap Gdiplus :: * ) bmp.GetThumbnailImage (SourceWidth, sourceHeight, NULL, NULL); HBITMAP hBMP; pThumbnail -> GetHBITMAP ( Color :: GDIPlus (LIST_BKCOLOR), & hBMP); the CBitmap * = the pImage the CBitmap :: FromHandle (hBMP); m_imageList.Add (the pImage, the RGB ( 255 , 0 , 255 )); // the following code, and if not, will memory leak Delete pThumbnail; pThumbnail = NULL; The pImage -> the DeleteObject (); The pImage ->DeleteTempMap(); }
Do not forget to initialize gdi, add initialization code in CXXXApp :: InitInstance () in
//初始化Gdi+ Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
In CXXXApp :: ExitInstance () in the code to add closed gdi
Gdiplus::GdiplusShutdown(m_gdiplusToken);
Add the declaration m_gdiplusToken in the header file CXXXApp's
ULONG_PTR m_gdiplusToken;