WIC工厂在Windows7上始终为nullptr(使用“什么是Creel?”教程)

问题描述 投票:0回答:1

我使用了this教程,但始终收到一个错误,指出“ wicFactory为nullptr”。我正在使用Windows 7,并且此代码无法正常工作。我读过您应该使用CLSID_WICImagingfactory1,但是它不起作用。这是我的代码:

HRESULT hr;
bmp = nullptr;

IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICFormatConverter *pConverter = NULL;

//Creating a factory
IWICImagingFactory *wicFactory = NULL;
CoCreateInstance(
    CLSID_WICImagingFactory1,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IWICImagingFactory,
    (LPVOID*)&wicFactory);

MessageBox(NULL, (LPCWSTR)wicFactory, NULL, MB_OK);

//Creating a decoder

hr = wicFactory->CreateDecoderFromFilename(
    filename,
    NULL,
    GENERIC_READ,
    WICDecodeMetadataCacheOnLoad,
    &pDecoder);

//Read Frame from image
if (SUCCEEDED(hr)) {
    // Create the initial frame.
    hr = pDecoder->GetFrame(0, &pSource);
}

//Creating a Converter
if (SUCCEEDED(hr)) {

    // Convert the image format to 32bppPBGRA
    // (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
    hr = wicFactory->CreateFormatConverter(&pConverter);
}

//Setup the converter
if (SUCCEEDED(hr)) {
    hr = pConverter->Initialize(
        pSource,
        GUID_WICPixelFormat32bppPBGRA,
        WICBitmapDitherTypeNone,
        NULL,
        0.0f,
        WICBitmapPaletteTypeMedianCut
    );
}
//Use the converter to create an D2D1Bitmap
//ID2D1Bitmap* bmp;
if (SUCCEEDED(hr))
{
    hr = d2dg->pRT->CreateBitmapFromWicBitmap(
        pConverter,
        NULL,
        &bmp
    );
}

if (wicFactory)wicFactory->Release();
if (pDecoder)pDecoder->Release();
if (pConverter)pConverter->Release();
if (pSource)pSource->Release();

[任何人都可以在这里找到问题吗?我找不到有关该问题的任何其他信息,然后将_WIN32_WINNT定义为0x0600或0x0601,但仍然无济于事...

c++ null direct2d nullptr wic
1个回答
0
投票

如注释中所述,您需要先致电CoInitializeEx,然后才能致电CoCreateInstance

顺便说一句,这是一些健壮的代码,可用于创建WIC工厂,以处理Windows 7的“ WIC2”与“ WIC1”下级场景,无论是否具有KB更新:

namespace
{
    bool g_WIC2 = false;
    IWICImagingFactory* g_Factory = nullptr;

    BOOL WINAPI InitializeWICFactory(PINIT_ONCE, PVOID, PVOID *ifactory) noexcept
    {
    #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE)
        HRESULT hr = CoCreateInstance(
            CLSID_WICImagingFactory2,
            nullptr,
            CLSCTX_INPROC_SERVER,
            __uuidof(IWICImagingFactory2),
            ifactory
        );

        if (SUCCEEDED(hr))
        {
            // WIC2 is available on Windows 10, Windows 8.x, and Windows 7 SP1 with KB 2670838 installed
            g_WIC2 = true;
            return TRUE;
        }
        else
        {
            g_WIC2 = false;

            hr = CoCreateInstance(
                CLSID_WICImagingFactory1,
                nullptr,
                CLSCTX_INPROC_SERVER,
                __uuidof(IWICImagingFactory),
                ifactory
            );
            return SUCCEEDED(hr) ? TRUE : FALSE;
        }
    #else
        g_WIC2 = false;

        return SUCCEEDED(CoCreateInstance(
            CLSID_WICImagingFactory,
            nullptr,
            CLSCTX_INPROC_SERVER,
            __uuidof(IWICImagingFactory),
            ifactory)) ? TRUE : FALSE;
    #endif
    }
}

IWICImagingFactory* DirectX::GetWICFactory(bool& iswic2) noexcept
{
    if (g_Factory)
    {
        iswic2 = g_WIC2;
        return g_Factory;
    }

    static INIT_ONCE s_initOnce = INIT_ONCE_STATIC_INIT;

    InitOnceExecuteOnce(&s_initOnce,
        InitializeWICFactory,
        nullptr,
        reinterpret_cast<LPVOID*>(&g_Factory));

    iswic2 = g_WIC2;
    return g_Factory;
}

您可以在Windows 7上使用WIC2(如果已安装,可以通过使用/D_WIN32_WINNT=0x0601 /D_WIN7_PLATFORM_UPDATE进行构建来启用。

© www.soinside.com 2019 - 2024. All rights reserved.