Introduction
Loading images under windows can always be a painful process, especially if the image formats are like JPEG, GIF or PNG. There's always the Imgdecmp library, that does a good job, but its also a bit of a bother with the documentation being so thin and all that. The other day I was trying to load a bitmap from my device using LoadImage
, but somehow I kept getting the dreaded "The handle is invalid" error. So I did a search on the MFC code that ships with the PPC SDK in hope of finding a piece of code that will help me solve the puzzle.
One thing led to another. I suddenly came across HBITMAP SHLoadImageFile(LPCTSTR pszFileName)
in aygshell.h. Bingo! I had suddenly uncovered an undocumented API! No trace of it in MSDN, a search in Google produced nothing!! Since then, I've tried this API with gif's and bmp's also and I guess it will work fine for jpeg's and png's too. Here's a code sample...
// assuming code is in the OnDraw method of a view....
CBitmap bitmap;
// Attach the bitmap object to the HBITMAP returned by SHLoadImageFile
bitmap.Attach(SHLoadImageFile(_T("/My Documents/mcdonalds.gif")));
BITMAP bmpInfo;
bitmap.GetBitmap(&bmpInfo);
CDC bitmapDC;
bitmapDC.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = bitmapDC.SelectObject(&bitmap);
pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &bitmapDC,
0, 0, SRCCOPY);
bitmapDC.SelectObject(pOldBitmap);
bitmap.DeleteObject();
That's all for now.