Solved

Bitmaps and memory handling

Posted on 1998-06-23
9
187 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dialogbox API leak? 18 96
Need an intro to -- .Net SQL Authorization Manager 7 88
How do ASP.NET and MVC work together? 4 48
Where did System.Data.Objects go? 2 29
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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