Solved

Bitmaps and memory handling

Posted on 1998-06-23
9
185 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 200 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now