Creating a HBITMAP from an existing buffer

大兔子大兔子 提交于 2019-12-11 18:06:59

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/21937095/creating-a-hbitmap-from-an-existing-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!