It's easy to produce IWICBitmap from HBITMAP with CreateBitmapFromHBITMAP factory method. But how to get simple GDI bitmap from IWICBitmapSource?

有帮助吗?

解决方案

Unfortulately there is no way to convert a IWICBitmap to a HBITMAP, or to get one from a IWICBitmapSource. You could, however, easily create the HBITMAP from a pixel buffer if the format is compatible with what CreateBitmap expects.

Assuming a 32-bit RGBA bitmap:

IWICBitmapSource *bitmap_source = some_func();

UINT width = 0, height = 0;
bitmap_source->GetSize(&width, &height);

std::vector<BYTE> buffer(width * height * 4);
bitmap_source->CopyPixels(0, width * 4, buffer.size(), buffer.data());

HBITMAP bitmap = CreateBitmap(width, height, 1, 32, buffer.data());

其他提示

There's no guarantee that behind the scenes IWICBitmapSource even uses a GDI bitmap. You can access the raw bits using the IWICBitmap::Lock method, but if you want to use the bitmap with GDI functions I think you will need to create a GDI bitmap and copy the bits to it yourself.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top