Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从HBITMAP转换为IWICBitmap_C++_Visual Studio 2010_Image_Bitmap_Wic - Fatal编程技术网

C++ 从HBITMAP转换为IWICBitmap

C++ 从HBITMAP转换为IWICBitmap,c++,visual-studio-2010,image,bitmap,wic,C++,Visual Studio 2010,Image,Bitmap,Wic,我正在尝试将HBITMAP转换为IWICBitmap,但遇到了不少麻烦。我发现这个函数: CreateBitmapFromHBITMAP(); 但我不能让它工作。以下是我如何使用它: void camera_avtcam_ex_t::GrabAsyncFrame(ULONG frameId, IWICImagingFactory* pWicFactory, IWICBitmap** outputBitmap, bool* pAbort ) { QueueCamFrame();

我正在尝试将HBITMAP转换为IWICBitmap,但遇到了不少麻烦。我发现这个函数:

CreateBitmapFromHBITMAP();
但我不能让它工作。以下是我如何使用它:

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);

}
执行函数中的最后一行代码会导致访问冲突错误。

在调用此
GrabAsyncFrame()
函数之前,我创建了它所需的参数,如下所示:

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

        theCamera.GrabAsyncFrame(frameId, pWicFactory, outputBitmap, pAbort);
我有点怀疑将pWicFactory设置为NULL,然后很快使用它。但是我想不出任何其他方法来初始化iWicMagingFactory对象

所以我的问题是:新问题发布在下面

编辑:如果我尝试使用
new
初始化pWicFactory,我会收到一条消息说

错误:不允许抽象类类型为“IwicMagingFactory”的对象

编辑2:

在确认将pWicFactory设置为NULL是问题之后,我现在需要知道如何正确初始化IwicMagingFactory对象指针。这就是我现在的工作:

            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);
问题:如何正确初始化iWicMagingFactory对象指针?

此声明

IWICImagingFactory* pWicFactory = NULL;
是罪魁祸首


您正在向函数传递一个
NULL
指针,然后尝试使用该指针,从而导致了错误。

除了NULL指针问题之外,您可能忘记了先调用
CoInitialize

IWICImagingFactory* Factory;

请注意,如果将工厂指针保持在
ComPtr
(我建议这样做),则需要在取消初始化之前释放工厂接口。在这种情况下,您应该:

ComPtr<IWICImagingFactory> Factory;
还要确保检查由
CoInitialize
CoCreateInstance
返回的
HRESULT
(为简洁起见,此处省略)


编辑:我现在在评论中看到这确实是你的问题。不过,我会留下我的答案,以防其他人和我一样草率行事。

好吧,正如我在问题中所说的,我觉得这就是问题所在,但要回答我的问题,有什么更好的方法来初始化它呢?使用
new
是我能想到的唯一其他方法,但我做不到@xcdemon05所以你的问题不是关于你的错误,而是如何创建这个对象?@JoachimPileborg我的问题是为什么这个不起作用。我感觉初始化为NULL是问题的一部分,但我不确定。现在知道了这一点,我接下来的问题是如何创建对象。好的,我建议您编辑原始问题以显示最新的代码,然后我们可以看到您现在在做什么。那么CoCreateInstance调用失败了?将返回值指定给HRESULT变量,并在调试器中查看其说明,然后使用该说明编辑问题。Aha。在使用
CoCreateInstance()
之前,我只需要调用
CoInitialize(NULL)
。现在一切正常:)
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.