如何将 IWICBitmapSource 转换为 HBITMAP?

And*_*rew 2 c++ windows winapi wic

使用工厂方法可以很容易地从 HBITMAP 生成 IWICBitmap CreateBitmapFromHBITMAP。但是如何从 IWICBitmapSource 获取简单的 GDI 位图呢?

sla*_*ppy 5

不幸的是,没有办法将 a 转换IWICBitmap为 a HBITMAP,或从 a 获取 1 IWICBitmapSource。但是,HBITMAP如果格式与预期兼容,您可以轻松地从像素缓冲区创建CreateBitmap

假设一个 32 位 RGBA 位图:

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());
Run Code Online (Sandbox Code Playgroud)