Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

Aside from the nullpointer issue, you probably forgot to call CoInitialize first:

IWICImagingFactory* Factory;

...

CoInitializeEx(NULL, COINIT_MULTITHREADED); // do this during program init / before CoCreateInstance

CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&Factory));

// use factory..

CoUninitialize(); // do this before program exit.

Note that if you keep your factory pointer in a ComPtr (which I recommend), you need to release the factory interface before uninitializing. In that case you should do:

ComPtr<IWICImagingFactory> Factory;

...

CoInitializeEx(NULL, COINIT_MULTITHREADED); // do this during program init / before CoCreateInstance

CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&Factory));

// use factory..

Factory.Reset(); // do this before CoUninitialize

CoUninitialize(); // do this before program exit.

Also be sure to check the HRESULT returned by CoInitialize and CoCreateInstance (omitted here for brevity)...

EDIT: I now see in a comment that this was indeed your problem. I'll leave my answer though, in case others are as sloppy-sighted as me..

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top