Copying a bitmap into a memory DC

Copying a bitmap into a memory DC

Post by Mehdi Mousav » Tue, 08 Jan 2002 17:28:22



I've got a handle to a bitmap, say, hBitmap. How can I copy the bitmap
associated with the hBitmap into hMemDC?

I've done something like this:

//note the casting please
BitBlt(hMemDC, x, y, 48, 48, (HDC)hBitmap, 0, 0, SRCCOPY);

But it seems too crazy to cast an HGDIOBJ into HDC!

Any help would be highly appreciated,

Cheers
Mehdi

 
 
 

Copying a bitmap into a memory DC

Post by John Hornick [M » Tue, 08 Jan 2002 22:51:38


Hi,

Quote:> I've got a handle to a bitmap, say, hBitmap. How can I copy the bitmap
> associated with the hBitmap into hMemDC?

> I've done something like this:

> //note the casting please
> BitBlt(hMemDC, x, y, 48, 48, (HDC)hBitmap, 0, 0, SRCCOPY);

> But it seems too crazy to cast an HGDIOBJ into HDC!

You don't "copy" a bitmap into a DC, you "select" it.
HBITMAP hOldBitmap = (HBITMAP)SelectObject( hMemDC, hBitmap );

Once you've selected the bitmap into the DC, you can BitBlt() to and from
that DC, or draw on it, like this for example:
BitBlt( hMemDC, 0, 0, 100, 100, hScreenDC, 0, 0, SRCCOPY ); // copy screen
to memory DC/bitmap
FillRect( hMemDC, &rc, hBrush ); // draw on memory DC/bitmap
BitBlt( hScreenDC, 0, 0, 100, 100, hMemDC, 0, 0, SRCCOPY ); // copy memory
DC/bitmap to screen

Then later you restore the DC to its previous state with another
SelectObject() call:
SelectObject( hMemDC, hOldBitmap );

Check out Charles Petzold's Programming Windows books for more information
on using GDI.

Thanks,
- John
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.

 
 
 

Copying a bitmap into a memory DC

Post by E. B. Wintermut » Sun, 13 Jan 2002 07:45:37


Mr. Hornick,

        I'm having a related problem which I suspect is due to the way I'm
(not) freeing memory. I'm blitting using the method you describe, but after
4-5 seconds of my game loop (I'm re-writing Pong as an exercise), the
bitmaps just disappear. I don't get any crashes or anything, everything
runs fine for a couple seconds and then the bitmaps just don't show up
anymore. Can you advise?

Thanks,
E.B.

HDC pad1 = CreateCompatibleDC(hdc);
HBITMAP padmap1  
=(HBITMAP)LoadImage(hInst,"Paddle1.bmp",IMAGE_BITMAP,45,142,LR_LOADFROMFILE
);
HBITMAP oldpad1 = (HBITMAP)SelectObject(pad1, padmap1);
TransparentBlt(mem_hdc,0,0,45,142,pad1,0,0,45,142,RGB(0,0,0));
SelectObject(pad1, oldpad1);
DeleteObject(padmap1);
DeleteDC(pad1);



Quote:> You don't "copy" a bitmap into a DC, you "select" it.
> HBITMAP hOldBitmap = (HBITMAP)SelectObject( hMemDC, hBitmap );

> Once you've selected the bitmap into the DC, you can BitBlt() to and
> from that DC, or draw on it, like this for example:
> BitBlt( hMemDC, 0, 0, 100, 100, hScreenDC, 0, 0, SRCCOPY ); // copy
> screen to memory DC/bitmap
> FillRect( hMemDC, &rc, hBrush ); // draw on memory DC/bitmap
> BitBlt( hScreenDC, 0, 0, 100, 100, hMemDC, 0, 0, SRCCOPY ); // copy
> memory DC/bitmap to screen

> Then later you restore the DC to its previous state with another
> SelectObject() call:
> SelectObject( hMemDC, hOldBitmap );

> Check out Charles Petzold's Programming Windows books for more
> information on using GDI.

> Thanks,
> - John
> Microsoft Developer Support
> This posting is provided "AS IS" with no warranties, and confers no
> rights.

 
 
 

Copying a bitmap into a memory DC

Post by John Hornick [M » Sun, 13 Jan 2002 08:18:27


Hi,

Quote:>            I'm having a related problem which I suspect is due to the way I'm
> (not) freeing memory. I'm blitting using the method you describe, but
after
> 4-5 seconds of my game loop (I'm re-writing Pong as an exercise), the
> bitmaps just disappear. I don't get any crashes or anything, everything
> runs fine for a couple seconds and then the bitmaps just don't show up
> anymore. Can you advise?

> Thanks,
> E.B.

> HDC pad1 = CreateCompatibleDC(hdc);
> HBITMAP padmap1  

=(HBITMAP)LoadImage(hInst,"Paddle1.bmp",IMAGE_BITMAP,45,142,LR_LOADFROMFILE

> );
> HBITMAP oldpad1 = (HBITMAP)SelectObject(pad1, padmap1);
> TransparentBlt(mem_hdc,0,0,45,142,pad1,0,0,45,142,RGB(0,0,0));
> SelectObject(pad1, oldpad1);
> DeleteObject(padmap1);
> DeleteDC(pad1);



> > You don't "copy" a bitmap into a DC, you "select" it.
> > HBITMAP hOldBitmap = (HBITMAP)SelectObject( hMemDC, hBitmap );

> > Once you've selected the bitmap into the DC, you can BitBlt() to and
> > from that DC, or draw on it, like this for example:
> > BitBlt( hMemDC, 0, 0, 100, 100, hScreenDC, 0, 0, SRCCOPY ); // copy
> > screen to memory DC/bitmap
> > FillRect( hMemDC, &rc, hBrush ); // draw on memory DC/bitmap
> > BitBlt( hScreenDC, 0, 0, 100, 100, hMemDC, 0, 0, SRCCOPY ); // copy
> > memory DC/bitmap to screen

> > Then later you restore the DC to its previous state with another
> > SelectObject() call:
> > SelectObject( hMemDC, hOldBitmap );

> > Check out Charles Petzold's Programming Windows books for more
> > information on using GDI.

> > Thanks,
> > - John
> > Microsoft Developer Support
> > This posting is provided "AS IS" with no warranties, and confers no
> > rights.

You don't mention where mem_hdc and hdc come from and how they're cleaned
up.

You could run under BoundsChecker or similar tool, and you should check the
return values from the functions to see which one is failing.

You could also, if running under XP or Win2K, choose to view GDI resources
in the Processes tab of Task Manager and see whether the problem is
consumption (leaking) of GDI resources.

Thanks,
- John
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.

 
 
 

Copying a bitmap into a memory DC

Post by E. B. Wintermut » Sun, 13 Jan 2002 08:41:15


Thanks for your reply. DeleteDC() hasn't returned an error as yet. I
haven't learned how to use BoundsChecker yet, but will look into it. Here's
a more complete exercpt of my code:

// Here's where the hdc's come from (beginning of the loop):
HDC hdc, mem_hdc;
HBITMAP mem_hbitmap, old_hbitmap;
RECT rt;

hdc = GetDC(hWnd);
mem_hdc = CreateCompatibleDC(hdc);
mem_hbitmap = CreateCompatibleBitmap(hdc, rt.right, rt.bottom);
old_hbitmap = (HBITMAP)SelectObject(mem_hdc, mem_hbitmap);

// The blitting code from my previous post:
HDC pad1 = CreateCompatibleDC(hdc);
        HBITMAP padmap1 =
(HBITMAP)LoadImage(hInst,"Paddle1.bmp",IMAGE_BITMAP,45,142,LR_LOADFROMFILE)
;
        HBITMAP oldpad1 = (HBITMAP)SelectObject(pad1, padmap1);
        TransparentBlt(mem_hdc,0,0,45,142,pad1,0,0,45,142,RGB(0,0,0));
        SelectObject(pad1, oldpad1);
        DeleteObject(padmap1);
        DeleteDC(pad1);

// The main blit function call, rt was passed to GetClientRect():
BitBlt(hdc, rt.left, rt.top, rt.right, rt.bottom, mem_hdc, rt.left, rt.top,
SRCCOPY);

// Here's the cleanup (end of the loop):
SelectObject(mem_hdc, old_hbitmap);
DeleteObject(mem_hbitmap);
DeleteDC(mem_hdc);
ReleaseDC(hWnd, hdc);

Quote:> You don't mention where mem_hdc and hdc come from and how they're
> cleaned up.

> You could run under BoundsChecker or similar tool, and you should check
> the return values from the functions to see which one is failing.

> You could also, if running under XP or Win2K, choose to view GDI
> resources in the Processes tab of Task Manager and see whether the
> problem is consumption (leaking) of GDI resources.

> Thanks,
> - John
> Microsoft Developer Support
> This posting is provided "AS IS" with no warranties, and confers no
> rights.

 
 
 

Copying a bitmap into a memory DC

Post by E. B. Wintermut » Sun, 13 Jan 2002 08:31:49


Thanks for your reply. DeleteDC() hasn't returned an error as yet. I
haven't learned how to use BoundsChecker yet, but will look into it. Here's
a more complete exercpt of my code:

// Here's where the hdc's come from (beginning of the loop):
HDC hdc, mem_hdc;
HBITMAP mem_hbitmap, old_hbitmap;
RECT rt;

hdc = GetDC(hWnd);
mem_hdc = CreateCompatibleDC(hdc);
mem_hbitmap = CreateCompatibleBitmap(hdc, rt.right, rt.bottom);
old_hbitmap = (HBITMAP)SelectObject(mem_hdc, mem_hbitmap);

// The blitting code from my previous post:
HDC pad1 = CreateCompatibleDC(hdc);
        HBITMAP padmap1 =
(HBITMAP)LoadImage(hInst,"Paddle1.bmp",IMAGE_BITMAP,45,142,LR_LOADFROMFILE)
;
        HBITMAP oldpad1 = (HBITMAP)SelectObject(pad1, padmap1);
        TransparentBlt(mem_hdc,0,0,45,142,pad1,0,0,45,142,RGB(0,0,0));
        SelectObject(pad1, oldpad1);
        DeleteObject(padmap1);
        DeleteDC(pad1);

// The main blit function call, rt was passed to GetClientRect():
BitBlt(hdc, rt.left, rt.top, rt.right, rt.bottom, mem_hdc, rt.left, rt.top,
SRCCOPY);

// Here's the cleanup (end of the loop):
SelectObject(mem_hdc, old_hbitmap);
DeleteObject(mem_hbitmap);
DeleteDC(mem_hdc);
ReleaseDC(hWnd, hdc)

Quote:> You don't mention where mem_hdc and hdc come from and how they're
> cleaned up.

> You could run under BoundsChecker or similar tool, and you should check
> the return values from the functions to see which one is failing.

> You could also, if running under XP or Win2K, choose to view GDI
> resources in the Processes tab of Task Manager and see whether the
> problem is consumption (leaking) of GDI resources.

> Thanks,
> - John
> Microsoft Developer Support
> This posting is provided "AS IS" with no warranties, and confers no
> rights.

 
 
 

Copying a bitmap into a memory DC

Post by John Hornick [M » Wed, 16 Jan 2002 01:29:14


Hi,

Quote:> Thanks for your reply. DeleteDC() hasn't returned an error as yet. I
> haven't learned how to use BoundsChecker yet, but will look into it.
Here's
> a more complete exercpt of my code:

> // Here's where the hdc's come from (beginning of the loop):
> HDC hdc, mem_hdc;
> HBITMAP mem_hbitmap, old_hbitmap;
> RECT rt;

> hdc = GetDC(hWnd);
> mem_hdc = CreateCompatibleDC(hdc);
> mem_hbitmap = CreateCompatibleBitmap(hdc, rt.right, rt.bottom);
> old_hbitmap = (HBITMAP)SelectObject(mem_hdc, mem_hbitmap);

> // The blitting code from my previous post:
> HDC pad1 = CreateCompatibleDC(hdc);
>    HBITMAP padmap1 =

(HBITMAP)LoadImage(hInst,"Paddle1.bmp",IMAGE_BITMAP,45,142,LR_LOADFROMFILE)

- Show quoted text -

Quote:> ;
>    HBITMAP oldpad1 = (HBITMAP)SelectObject(pad1, padmap1);
>    TransparentBlt(mem_hdc,0,0,45,142,pad1,0,0,45,142,RGB(0,0,0));
>    SelectObject(pad1, oldpad1);
>    DeleteObject(padmap1);
>    DeleteDC(pad1);

> // The main blit function call, rt was passed to GetClientRect():
> BitBlt(hdc, rt.left, rt.top, rt.right, rt.bottom, mem_hdc, rt.left,
rt.top,
> SRCCOPY);

> // Here's the cleanup (end of the loop):
> SelectObject(mem_hdc, old_hbitmap);
> DeleteObject(mem_hbitmap);
> DeleteDC(mem_hdc);
> ReleaseDC(hWnd, hdc)

> > You don't mention where mem_hdc and hdc come from and how they're
> > cleaned up.

> > You could run under BoundsChecker or similar tool, and you should check
> > the return values from the functions to see which one is failing.

> > You could also, if running under XP or Win2K, choose to view GDI
> > resources in the Processes tab of Task Manager and see whether the
> > problem is consumption (leaking) of GDI resources.

> > Thanks,
> > - John
> > Microsoft Developer Support
> > This posting is provided "AS IS" with no warranties, and confers no
> > rights.

The code above, pasted into a loop and run on my machine does not leak.

Hopefully BoundsChecker will help you locate the leak, if you have one.
Otherwise, you may need to start commenting out sections of code until you
find the offending code.

Thanks,
- John
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.

 
 
 

Copying a bitmap into a memory DC

Post by E. B. Wintermut » Wed, 16 Jan 2002 02:34:57




Thanks again.

Damn. I commented out all but the one bitmap drawing routine I included in
my last post, and it still disappears after a few seconds. I also did some
error checking as you suggested and determined that the call to
TransparentBlit() is failing, though I have no idea why.

If I can't figure this out soon, I'm going to have to adopt an alternative
bitmap drawing scheme. As far as I can tell I'm doing everything right, but
the bitmaps still drop out on me... very irritating.

Thanks,
Evan

Quote:> Hi,
> The code above, pasted into a loop and run on my machine does not leak.

> Hopefully BoundsChecker will help you locate the leak, if you have one.
> Otherwise, you may need to start commenting out sections of code until
> you find the offending code.

> Thanks,
> - John
> Microsoft Developer Support
> This posting is provided "AS IS" with no warranties, and confers no
> rights.

 
 
 

Copying a bitmap into a memory DC

Post by John Hornick [M » Wed, 16 Jan 2002 07:57:53


Hi,

Quote:> Thanks again.

> Damn. I commented out all but the one bitmap drawing routine I included
in
> my last post, and it still disappears after a few seconds. I also did
some
> error checking as you suggested and determined that the call to
> TransparentBlit() is failing, though I have no idea why.

> If I can't figure this out soon, I'm going to have to adopt an
alternative
> bitmap drawing scheme. As far as I can tell I'm doing everything right,
but
> the bitmaps still drop out on me... very irritating.

> Thanks,
> Evan

> > Hi,
> > The code above, pasted into a loop and run on my machine does not leak.

> > Hopefully BoundsChecker will help you locate the leak, if you have one.
> > Otherwise, you may need to start commenting out sections of code until
> > you find the offending code.

If you are on an NT OS (NT4/2K/XP), use GetLastError() after the
TransparentBlt() failure - it may tell you why the function failed.

Also, if any of your parameters to TransparentBlt() are definined outside
the loop, monitor their values to see if they change in the failure case.

Thanks,
- John
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.

 
 
 

1. copying a bitmap into a memory DC

I am having trouble taking a bitmap in a DC and moving it into a
memory DC and then moving it back.

When I BitBlt the memDC back into bitmapDC the image is scrambled?

Can anyone tell me where I am going wrong?

A sample seg of code follows:

hDCMemory = CreateCompatibleDC(hDCBitMap1)  'create a memory device context
hbm = CreateCompatibleBitmap(hDCBitMap1, 250, 250) 'create compatible bitmap DC
hBMPOld = SelectObject(hDCMemory, hbm) 'set memory DC to bitmap
Call BitBlt(hDCBitMap1, 0, 0, 150, 150, hDCMemory, 0, 0, SRCCOPY)

Thanks,
Jay

2. WANTED: USR Sportster 14.4k baud

3. MFC: Help needed copying images from memory DC to display DC

4. False Virus Warning??

5. MFC: Copying images from memory DC to display DC?

6. Brightness

7. Printer DC's and Enhanced Metafile DC's and Memory DC's

8. my ICS settings HELP!!!

9. Copying a DC to a Bitmap?

10. Copying color bitmaps to a B&W DC

11. DC copy in memory block

12. How to copy a window's DC to another DC?

13. Memory DC, Bitmaps, and Text