I have an existing buffer full of (DIB) bitmap data, i.e. width x height x 4 bytes (RGBA) in size. What I want to do is draw this bitmap to the screen, but looking at the CreateBitmap... / CreateDIB... functions, they don't appear to do what I'm looking for. I don't want to copy the memory in, I want to retain access to it, so I can continue to write to it in the next frame (without incurring a penalty for copying the data). Does such a method exist, or do I have to create a new bitmap and call SetDIBits on it?
-
Do you have a raw char buffer and want to draw it on screen? – γηράσκω δ' αεί πολλά διδασκόμε Feb 21 '14 at 17:39
2 Answers
If you want simple code, then you can use a BITMAP structure and assign it's bmBits to point to your actual image data (RGBA 8-Bit).
Then you can use GDI method
HBITMAP CreateBitmapIndirect(const BITMAP *pbm);
to create HBITMAP for displaying the image to screen.
But actually I think the system still do the copying while creating HBITMAP, that's why after CreateBitmapIndirect returns, you can safely free your image data.
But at least you only have to create the buffer once, and using it repeatedly as long as the size of the image doesn't change.
I use this method to display raw video from RED's Camera.

- 11
- 3
You can't write a DIB directly to a device context - you'll have to create a bitmap and copy the pixels in. Annoying, I know!
Looks like this question has a succinct way of doing that in the accepted answer.
-
Yeah I'd seen that question, but unfortunately the accepted answer uses GDI+ which I can't use. – Mark Ingram Feb 21 '14 at 15:04
-
That's a shame. Well it's `SetDIBits` and possible pixel conversion for you then!! Hope it goes well. – noelicus Feb 21 '14 at 15:09