It has taken me a little time, but I have narrowed my problem down to
CBitmap::GetObject.
My application loads many images. The frequently used ones have been
converted to bitmaps and are loaded as resources, but the rest are still in
JPEG format on the device's Compact Flash storage card.
To load a JPEG, I use SHLoadImageFile. To load a resource bitmap, I use
LoadBitmap (see code below).
The problem comes when I attempt to call GetObject on a jpeg file. The image
displays correctly, but parts of my program get overwritten whenever it
happens. (Currently, CStringList files is being overwritten, but if the
declaration order gets changed around somewhat, it will go to something
else.)
Could someone look at my use of GetObject and tell me what I am doing wrong?
Could you suggest a way to fix it?
void CForm1Dlg::OnPaint()
{
CBitmap bitmap, *pOldbmp;
CPaintDC dc(this);
BITMAP BMP;
CDC mDC;
DWORD ok;
HBITMAP hBMP;
if (bImageIsJpegFile)
hBMP = SHLoadImageFile(szImageFile);
else
hBMP = LoadBitmap(AfxGetApp()->m_hInstance,
MAKEINTRESOURCE(IDB_IMAGE2));
if (hBMP == NULL)
return;
ok = bitmap.Attach(hBMP);
if (!ok)
return;
// Help says if I pass NULL as param2, GetObject returns the
// size needed to allocate for the bitmap object:
ok = bitmap.GetObject(sizeof(BITMAP), NULL);
// problem is, GetObject above always returns ok=84.
// Any idea what that would mean?
if (!ok)
return;
// In the GetObject below, if hBMP is to a JPEG file,
// other areas of my application get overwritten.
// Does anyone know why?
ok = bitmap.GetObject(ok, &BMP);
// CStringList files was 56, now it is 0.
if (!ok)
return;
mDC.CreateCompatibleDC(&dc);
pOldbmp = mDC.SelectObject(&bitmap);
if (pOldbmp != NULL)
{
dc.StretchBlt(r.left, r.top, r.Width(), r.Height(), &mDC,
0, 0, BMP.bmWidth, BMP.bmHeight, SRCCOPY);
mDC.SelectObject(pOldbmp);
mDC.DeleteDC();
}
mDC.ReleaseAttribDC();
}
Thanks in advance!
~Joe