ID2D1HwndRenderTarget :: CreateBitmapFromWicBitmap()

时间:2016-10-03 13:11:37

标签: c++ winapi direct2d

我正在尝试使用WIC从文件加载图片并使用Direct2D在屏幕上显示。我遵循MSDN example但我遇到了函数CreateBitmapFromWicBitmap()的问题。

无论我在ID2D1HwndRenderTarget创建和IWICFormatConverter::Initialize()函数调用期间使用pixel formats的哪种组合,函数CreateBitmapFromWicBitmap()都会返回0x88982f80错误(WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT)。

我使用与绘图相同的ID2D1HwndRenderTarget来调用函数。我应该创建另一个渲染目标吗?

在此link的评论部分,有人写道,CreateBitmapFromWicBitmap()应该由DXGI表面渲染目标调用。是否意味着此功能根本无法与ID2D1HwndRenderTarget一起使用?

编辑:

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就是这样创建的:

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);

1 个答案:

答案 0 :(得分:2)

CreateBitmapFromWicBitmap期望converter作为第一个参数,而不是frame

使用CLSID_WICImagingFactory代替CLSID_WICImagingFactory1。编译器将选择正确的值。在我的情况下,它选择CLSID_WICImagingFactory2

IWICImagingFactory* factory的变量名称隐藏了一个具有相同名称的全局变量。这可能不会导致错误,但更改它会更好......

需要释放句柄。

HRESULT LoadBitmapFromFile(const wchar_t *filename, ID2D1HwndRenderTarget* target, ID2D1Bitmap** pBitmap)
{
    HRESULT hr = S_FALSE;
    IWICImagingFactory* wic_factory = NULL;
    IWICBitmapDecoder* decoder = NULL;
    IWICBitmapFrameDecode* frame = NULL;
    IWICFormatConverter* converter = NULL;

    hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, reinterpret_cast<void**>(&wic_factory));
    if FAILED(hr) goto clenaup;

    hr = wic_factory->CreateDecoderFromFilename(filename, NULL, GENERIC_READ, WICDecodeMetadataCacheOnLoad, &decoder);
    if FAILED(hr) goto clenaup;

    hr = decoder->GetFrame(0, &frame);
    if FAILED(hr) goto clenaup;

    hr = wic_factory->CreateFormatConverter(&converter);
    if FAILED(hr) goto clenaup;

    hr = converter->Initialize(frame, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeMedianCut);
    if FAILED(hr) goto clenaup;

    hr = target->CreateBitmapFromWicBitmap(converter, 0, pBitmap);
    if FAILED(hr) goto clenaup;

clenaup:
    safe_release(decoder);
    safe_release(converter);
    safe_release(frame);
    safe_release(wic_factory);
    return hr;
}

CoInitializeEx(0, COINIT_MULTITHREADED)可以在初始化时调用一次。

ID2D1Factory* factory;
ID2D1HwndRenderTarget* target;

void initialize(HWND hwnd)
{
    CoInitializeEx(0, COINIT_MULTITHREADED);

    RECT rc;
    GetClientRect(hwnd, &rc);

    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(rc.right, rc.bottom)),
        &target);
}

void on_render()
{
    target->BeginDraw();
    target->Clear(D2D1::ColorF(D2D1::ColorF::White));

    ID2D1Bitmap* pBitmap = NULL;
    if (SUCCEEDED(LoadBitmapFromFile(L"filename.png", target, &pBitmap)))
    {
        D2D1_SIZE_F size = pBitmap->GetSize();
        target->DrawBitmap(pBitmap, D2D1::RectF(0, 0, size.width, size.height));
        safe_release(pBitmap);
    }

    target->EndDraw();
}