Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bitmaps and memory handling

Posted on 1998-06-23
9
Medium Priority
?
194 Views
Last Modified: 2013-12-03
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;
}
0
Comment
Question by:parlando
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1408433
You might want to change the

hDC    = GetDC(NULL);

to

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

That might fix it.
0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1408434
You need to select the bitmaps out of the DC before deleting the DC and before deleting the bitmaps.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1408435
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 22

Expert Comment

by:nietod
ID: 1408436
Thui beat me to it.  But he only points out one problem.  Notice there are two.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1408437
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
 
LVL 22

Expert Comment

by:nietod
ID: 1408438
I've seen it too.  In my own programs!  Win 95 is more foregiving, and that isn't necessarily a good thing.
0
 

Author Comment

by:parlando
ID: 1408439
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
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
ID: 1408440
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
 
LVL 22

Expert Comment

by:nietod
ID: 1408441
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question