Bitmaps and memory handling
Posted on 1998-06-23
I have written a 16-bit Windows application, in wich I need to copy part of a bitmap
to another bitmap. Therefore I use the function printed beneath.
In this function, hOrgBmp is a handle to a bitmap created from an application window.
pRect is a pointer to the rectangular region I need from this hOrgBmp.
hNewBmp has to be a handle to the newly created bitmap.
For debugging purposes I have added some lines to save the bitmap.
Only NewBmp1.bmp is saved to the disk, after the DeleteObject(hBmp); statement, I have lost
my hNewBmp !
The strange thing is that this only happens under Windows 3.x. Running under Window95 this code works perfectly.
I have had this problem once before in another function. I've solved it by making hBmp a global variable, and calling the DeleteObject(hBmp) statement after I no longer needed the bitmap handle to the new bitmap. (Are you still with me ?)
Therefore I believe it is kind of a memory thing, but WHAT ?
If anyone can help, I would be very grateful !
HBITMAP AlgCopyBmpToBmp(HBITMAP hOrgBmp, LPRECT pRect)
int iWidth, iHeight;
HDC hDC, hMemDC, hMemOrgDC;
HBITMAP hBmp, hNewBmp = NULL;
hDC = GetDC(NULL);
hMemDC = CreateCompatibleDC (hDC);
iWidth = pRect->right - pRect->left + 1;
iHeight = pRect->bottom - pRect->top + 1;
hBmp = CreateCompatibleBitmap(hDC, iWidth, iHeight);
hMemOrgDC = CreateCompatibleDC (hDC);
BitBlt(hMemDC, 0, 0, iWidth, iHeight, NULL, 0, 0, WHITENESS);
BitBlt(hMemDC, 0, 0, iWidth, iHeight,
hMemOrgDC, pRect->left, pRect->top, SRCCOPY);
hNewBmp = SelectObject(hMemDC, hBmp);