Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bitmaps and memory handling

Posted on 1998-06-23
9
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

719 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