Gunnar Vestergaard
Gunnar Vestergaard

Reputation: 53

How to initialize IWICBitmapDecoder with Windows Imaging Component API

Actually I belive that this problem is easy to solve. It only needs a minor correction. This is the first time that I use Windows Imaging Component (WIC). I am trying to display a PNG picture in my window using Win32 or the Windows API as Microsoft likes to call it. I have written code to initialize it. It requires many steps. I have included a PNG image in my .exe file. Maybe you can look at my code to see what is missing:

void ShowImage() {
    IWICBitmapDecoder** ppIDecoder;
    IStream* pIStream;
    HRESULT hr;

    ID2D1RenderTarget* pRenderTarget;
    IWICImagingFactory* pIWICFactory = 0;
    PCWSTR resourceName, resourceType;
    UINT destinationWidth;
    UINT destinationHeight;
    ID2D1Bitmap** ppBitmap;

    IWICBitmapDecoder* pDecoder = NULL;
    IWICBitmapFrameDecode* pSource = NULL;
    IWICBitmapSource* pISource;
    IWICStream* pStream = NULL;
    IWICFormatConverter* pConverter = NULL;
    IWICBitmapScaler* pScaler = NULL;

    HRSRC imageResHandle = NULL;
    HGLOBAL imageResDataHandle = NULL;
    void* pImageFile = NULL;
    DWORD imageFileSize = 0;

    // Locate the resource. Aaahhh I should have called Makeintresource and written "PNG"
    imageResHandle = FindResource(hInst, MAKEINTRESOURCE(103), L"PNG");
    hr = imageResHandle ? S_OK : E_FAIL;
    if (SUCCEEDED(hr)) {
        // Load the resource.
        imageResDataHandle = LoadResource(hInst, imageResHandle);

        hr = imageResDataHandle ? S_OK : E_FAIL;
    }
    if (SUCCEEDED(hr)) {
        // Lock it to get a system memory pointer.
        pImageFile = LockResource(imageResDataHandle);

        hr = pImageFile ? S_OK : E_FAIL;
    }
    if (SUCCEEDED(hr)) {
        // Calculate the size.
        imageFileSize = SizeofResource(hInst, imageResHandle);

        hr = imageFileSize ? S_OK : E_FAIL;

    }
    // call coinitialize IWICImagingFactory
    if(SUCCEEDED(hr))
        hr = CoInitializeEx(0, COINIT_MULTITHREADED);
    // Create WIC factory
    if (SUCCEEDED(hr)) {
        hr = CoCreateInstance(CLSID_WICImagingFactory,
            NULL,
            CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIWICFactory)
        );
    }
    if (SUCCEEDED(hr)) {
        // Create a WIC stream to map onto the memory.
        hr = pIWICFactory->CreateStream(&pStream);
    }
    if (SUCCEEDED(hr)) {
        // Initialize the stream with the memory pointer and size.
        hr = pStream->InitializeFromMemory(reinterpret_cast <BYTE*>(pImageFile),
            imageFileSize);
    }
    if (SUCCEEDED(hr)) {
        // Create a decoder for the stream.
        --->hr = pIWICFactory->CreateDecoderFromStream(pStream, 0, WICDecodeMetadataCacheOnLoad, ppIDecoder);
    }
}

I tried the above code which compiles without errors. All the function calls succeed except the last one that I marked with --->. Then the debugger shows a runtime exception because "The variable 'ppIDecoder' is being used without being initialized." How do I initialize ppIDecoder? As said, this problem should be quick to solve.

Upvotes: 0

Views: 291

Answers (0)

Related Questions