Search code examples
c++visual-studio-2010imagebitmapwic

Convert from HBITMAP to IWICBitmap


I'm trying to convert an HBITMAP to an IWICBitmap, and I'm having quite a bit of trouble. I found the function:

CreateBitmapFromHBITMAP();

but I can't get it to work. Here is how I am using it:

void camera_avtcam_ex_t::GrabAsyncFrame(ULONG frameId, IWICImagingFactory* pWicFactory, IWICBitmap** outputBitmap, bool* pAbort )
{

        QueueCamFrame();
        HBITMAP transferbitmap;
        GetFeatureAndRunAcquisitionStart(transferbitmap); //returns transferbitmap 
                                                          //as a valid HBITMAP
       //This HBITMAP works, I can save it to a file and/or print 
       //it to the screen and the image is displayed properly

        pWicFactory->CreateBitmapFromHBITMAP(transferbitmap, NULL, WICBitmapUseAlpha, outputBitmap);

}

Executing that last line of code in the function causes an access violation error.

Right before this GrabAsyncFrame() function is called, I create the parameters it needs like this:

        ULONG frameId = 0;
        IWICImagingFactory* pWicFactory = NULL;
        IWICBitmap** outputBitmap = new IWICBitmap*;
        bool* pAbort = NULL;

        theCamera.GrabAsyncFrame(frameId, pWicFactory, outputBitmap, pAbort);

I'm kind of suspect to setting pWicFactory to NULL, and then using it soon after. But I couldn't figure out any other way to initialize the IWICImagingFactory objects.

So my question is: New question is posted below.

EDIT: If I try using new to initialize pWicFactory, I get a message saying

Error: object of abstract class type "IWICImagingFactory" is not allowed.

EDIT2:

After confirming that setting pWicFactory to NULL was the problem, I now need to know how to properly initialize an IWICImagingFactory object pointer. This is what I'm working with now:

            ULONG frameId = 0;
            IWICImagingFactory* pWicFactory = NULL;
/*new code*/CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pWicFactory));
            IWICBitmap** outputBitmap = new IWICBitmap*;
            bool* pAbort = NULL;
            theCamera.GrabAsyncFrame(frameId, pWicFactory, outputBitmap, pAbort);

Question: How do I properly initialize an IWICImagingFactory object pointer?


Solution

  • This declaration

    IWICImagingFactory* pWicFactory = NULL;
    

    is the culprit.

    You're passing a NULL pointer to the function, which you then try to use, causing the error.