I am trying to use WIC to load picture from the file and display it on screen with Direct2D. I follow the MSDN example but I encounter a problem with the function CreateBitmapFromWicBitmap().
No matter which combination of pixel formats I use during ID2D1HwndRenderTarget
creation and IWICFormatConverter::Initialize()
function calling, function CreateBitmapFromWicBitmap()
returns 0x88982f80 error (WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
).
I call the function using the same ID2D1HwndRenderTarget
which I use for drawing. Should I create another render target?
In the comment section in this link, someone wrote that CreateBitmapFromWicBitmap()
should be called by DXGI surface render target. Does it mean that this function simply cannot be used with ID2D1HwndRenderTarget
?
EDIT:
void LoadBitmapFromFile(ID2D1HwndRenderTarget* target, ID2D1Bitmap** ppBitmap)
{
IWICImagingFactory* factory;
IWICBitmapDecoder* decoder;
IWICBitmapFrameDecode* frame;
IWICFormatConverter* converter;
CoInitializeEx(0, COINIT_MULTITHREADED);
CoCreateInstance(CLSID_WICImagingFactory1,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
reinterpret_cast<void**>(&factory));
factory->CreateDecoderFromFilename(L".png",
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&decoder);
decoder->GetFrame(0, &frame);
factory->CreateFormatConverter(&converter);
converter->Initialize(frame,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut);
target->CreateBitmapFromWicBitmap(frame, 0, ppBitmap);
}
ID2D1HwndRenderTarget is created like that:
ID2D1Factory* factory;
ID2D1HwndRenderTarget* target;
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factory);
factory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_HARDWARE,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)),
D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU(width, height)),
&target);