Share via

why is CreateBitmapFromWicBitmap throwing error?

Andres Ferrari 20 Reputation points
2023-10-11T15:15:17.1733333+00:00

So, I've wrote the following code but for some reason I am not aware of, CreateBitmapFromWicBitmap is throwing errors. Here's the code:

SpriteSheet::SpriteSheet(const wchar_t* filename, MainWindow* mw){

    bmap = NULL;
    HRESULT hr;
    IWICImagingFactory *wicFactory = NULL;

    hr = CoCreateInstance(CLSID_WICImagingFactory,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_IWICImagingFactory,
                          (LPVOID *)&wicFactory);

    if(hr != S_OK){
        return;
    }

    IWICBitmapDecoder *wicDecoder = NULL;

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

    if(hr != S_OK){
        return;
    }

    IWICBitmapFrameDecode *wicFrame = NULL;
    hr = wicDecoder->GetFrame(0,&wicFrame);
    if(!SUCCEEDED(hr)){
        std::cout<<"error"<<std::endl;
    }
    IWICFormatConverter *wicConverter;
    hr = wicFactory->CreateFormatConverter(&wicConverter);

    if(!SUCCEEDED(hr)){
        std::cout<<"error"<<std::endl;
    }
    hr = wicConverter->Initialize(wicFrame,
                                  GUID_WICPixelFormat32bppBGRA,
                                  WICBitmapDitherTypeNone,
                                  NULL,
                                  0.0,
                                  WICBitmapPaletteTypeCustom);

    hr = mw->GetRenderTarget()->CreateBitmapFromWicBitmap(
        wicConverter,
        NULL,
        &bmap
    );

    if(!SUCCEEDED(hr)){
        std::cout<<"error"<<std::endl;
    }

    if(wicFrame)     wicFrame->Release();
    if(wicFactory)   wicFactory->Release();
    if(wicDecoder)   wicDecoder->Release();
    if(wicConverter) wicConverter->Release();

 }

the error code is: 12160

Windows development | Windows API - Win32
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

Answer accepted by question author

Castorix31 91,876 Reputation points
2023-10-11T17:00:00.14+00:00

The format should be

GUID_WICPixelFormat32bppPBGRA

(not GUID_WICPixelFormat32bppBGRA)

(I copied/pasted your code, similar as MSDN code and it worked with this format, like in MS samples)

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.