Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ID2D1HwndRenderTarget::CreateBitmapFromWicBitmap()_C++_Winapi_Direct2d - Fatal编程技术网

C++ ID2D1HwndRenderTarget::CreateBitmapFromWicBitmap()

C++ ID2D1HwndRenderTarget::CreateBitmapFromWicBitmap(),c++,winapi,direct2d,C++,Winapi,Direct2d,我正在尝试使用WIC从文件中加载图片,并使用Direct2D将其显示在屏幕上。我遵循了,但函数CreateBitmapFromWicBitmap()遇到了一个问题 无论我在ID2D1HwndRenderTarget创建和IWICFormatConverter::Initialize()函数调用期间使用哪种组合,函数CreateBitmapFromWicBitmap()返回0x88982f80错误(WINCODEC\u ERR\u UNSUPPORTEDPIXELFORMAT) 我使用与绘图相同的

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

无论我在
ID2D1HwndRenderTarget
创建和
IWICFormatConverter::Initialize()
函数调用期间使用哪种组合,函数
CreateBitmapFromWicBitmap()
返回0x88982f80错误(
WINCODEC\u ERR\u UNSUPPORTEDPIXELFORMAT

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

在本文的注释部分,有人写道,DXGI曲面渲染目标应该调用
CreateBitmapFromWicBitmap()
。这是否意味着此函数不能与ID2D1HwndRenderTarget一起使用

编辑:


CreateBitmapFromWicBitmap
期望将
转换器
作为第一个参数,而不是

使用
CLSID\u WICImagingFactory
而不是
CLSID\u WICImagingFactory1
。编译器将选择正确的值。在我的例子中,它选择
CLSID\u WICImagingFactory2

iWicMagingFactory*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;
}

请以适当的方式显示一些代码。非常感谢您注意到CreateBitmapFromWicBitmap()中的参数问题。我永远也找不到它。但我发现了不同的东西。当我将CLSID_WICImagingFactory1更改为CLSID_WICImagingFactory时,CoCreateInstance()返回REGDB_E_CLASSNOTREG。我明白了。这里有关于
CLSID\u WICImagingFactory
的更多信息。我在Windows 10上的VS2015中,
CLSID_WICImagingFactory1
CLSID_WICImagingFactory2
工作正常。
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);
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;
}
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();
}