4
votes

I've managed to achieve double buffering with GDI, but not with GDI+. I would like to display a png image without having it flicker. Furthermore, at some point I would also like to achieve animation with png images using GDI+, so knowing how to double buffer with GDI + is imperative.

I have managed to get a png image into an Image object, via an ISTREAM: Here is a portion of my code that will help you understand where I'm having trouble:

memmove(pBlock,pImage, size);
CreateStreamOnHGlobal(hBlock, FALSE, &pStream);
Graphics graphics(memDC);
Image image(pStream);
int image_width;
int image_height;
image_width= image.GetWidth();
image_height=image.GetHeight();
graphics.DrawImage(&image, posX,posY, image_width, image_height);
BitBlt(hdc, 0, 0, image_width, image_height, memDC, 0, 0, SRCCOPY);

Note: If I draw the png image directly to the screen DC (hdc), it renders fine. However, when I attempt to draw the image to a memDC first, and then blt that memDC to the screenDC, no image appears!

Can someone please point me in the right direction as to how someone is to double buffer with GDI plus? Thank you

1
No error checking at all. So of course you don't know why it doesn't work.Hans Passant

1 Answers

2
votes

I think your problem might be in how you're creating your memDC - are you using CreateCompatibleDC() to create that to make sure it's the compatible with the hdc that you're doing the BitBlt() into?

I've answered a question similar to this before about double buffering before and you might find that answer helpful :

GDI Acceleration In Windows 7 / Drawing To Memory Bitmap

There's code there that I've used quite a lot to double buffer with GDI, but draw to the memory bitmap using either GDI or GDI+. I've found it really useful being able to use GDI as it is significantly quicker for some operations (especially bitmap related functions) than GDI+, but GDI+ does some things much more easily so it gives the best of both worlds.