Bitmaps and memory handling

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);
   if (hMemDC)
   {
     iWidth  = pRect->right - pRect->left + 1;
     iHeight = pRect->bottom - pRect->top + 1;
     hBmp = CreateCompatibleBitmap(hDC, iWidth, iHeight);
     if (hBmp)
     {
      hMemOrgDC = CreateCompatibleDC (hDC);
      if (hMemOrgDC)
      {
       SelectObject(hMemOrgDC, hOrgBmp);
       SelectObject(hMemDC, hBmp);
       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);
       SaveBitmap(hNewBmp, "NewBmp1.bmp");
       DeleteObject(hBmp);
       SaveBitmap(hNewBmp, "NewBmp2.bmp");
       DeleteDC(hMemOrgDC);
       SaveBitmap(hNewBmp, "NewBmp3.bmp");
      }
     }
     DeleteDC(hMemDC);
   }
   SaveBitmap(hNewBmp, "NewBmp4.bmp");

   return hNewBmp;
}
parlandoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
nietodConnect With a Mentor Commented:
I'm answering, for the moment.  but I want to point out that my answer was what thui proposed first (by a narrow margin)  You might want to reconsider--the choice is yours.
0
 
nietodCommented:
You might want to change the

hDC    = GetDC(NULL);

to

hDC    = CreateDC("DISPLAY",NULL,NULL,NULL);

That might fix it.
0
 
Tommy HuiEngineerCommented:
You need to select the bitmaps out of the DC before deleting the DC and before deleting the bitmaps.
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
nietodCommented:
A more serios problem, possibly the cause.

       hNewBmp = SelectObject(hMemDC, hBmp);
          SaveBitmap(hNewBmp, "NewBmp1.bmp");
          DeleteObject(hBmp);

The deleteobject() call deletes a bitmap that is currently selected into a DC.  BAD IDEA.  You must select it out first and then delete it.  Then you do

          SaveBitmap(hNewBmp, "NewBmp2.bmp");
          DeleteDC(hMemOrgDC);

Here the DeleteDC deletes a DC that does not have its original bitmap selected in.  BAD IDEA.  Select back the original bitmap before deleting the DC.
     
0
 
nietodCommented:
Thui beat me to it.  But he only points out one problem.  Notice there are two.
0
 
chensuCommented:
I noticed that some 16-bit applications with GDI bugs like these seem to work fine on Windows 95. But when you test them on Windows 3.x, you'll see the GPFs or some other problems come out.
0
 
nietodCommented:
I've seen it too.  In my own programs!  Win 95 is more foregiving, and that isn't necessarily a good thing.
0
 
parlandoAuthor Commented:
Sorry to've kept you waiting so long.
nietod's answer was a great help. The problem has been solved.
So nietod, if you'll answer (anything) again, I'll gice you the credits !
0
 
nietodCommented:
By the way.  Its been nearly a month.  I hope you haven't been struggling on this since then!  If so, you could have posted additional comments for additional help.  The comments/answers aren't necessarily the final word.
0
All Courses

From novice to tech pro — start learning today.