2013-03-15 51 views
0

我試圖將HBITMAP轉換爲IWICBitmap,並且遇到了相當多的麻煩。我發現的功能:從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()功能之前,我創造它需要像這樣的參數:我有種懷疑設定pWicFactory爲NULL,然後後使用它很快

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

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

。但我找不出任何其他方式來初始化IWICImagingFactory對象。

所以我的問題是:新問題發佈如下。

編輯:如果我嘗試使用new初始化pWicFactory,我得到一個消息說

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

EDIT2:

確認設置pWicFactory爲NULL是問題之後,我現在需要知道如何正確初始化IWICImagingFactory對象指針。這就是我與現在的工作:

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

問:如何正確初始化IWICImagingFactory對象的指針?

+0

因此,CoCreateInstance調用失敗?將返回值分配給HRESULT變量並在調試器中查看其描述,然後使用該描述編輯問題。 – user1610015 2013-03-15 14:05:42

+0

啊哈。我只需要在使用'CoCreateInstance()'之前調用'CoInitialize(NULL)'。現在工作正常:) – xcdemon05 2013-03-15 14:30:31

回答

1

這個聲明

IWICImagingFactory* pWicFactory = NULL; 

是罪魁禍首。

您正將一個NULL指針傳遞給該函數,然後嘗試使用該函數,從而導致該錯誤。

從空指針問題
+0

好吧,正如我在我的問題中所述,我有一種感覺是問題,但回答我的問題,什麼是更好的初始化方法?使用'new'是我能想到的唯一方法,但我不能那樣做。 – xcdemon05 2013-03-15 13:14:20

+0

Try - CoCreateInstance(CLSID_WICImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&pIWICFactory)); – 2013-03-15 13:16:36

+0

@ xcdemon05所以你的問題並不是真的關於你的錯誤,而是如何創建這個對象? – 2013-03-15 13:18:42

0

除此之外,你可能忘了打電話給CoInitialize第一:

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. 

請注意,如果你把你的工廠的指針在ComPtr(我建議),您需要在未初始化之前釋放工廠界面。在這種情況下,你應該做的:

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. 

而且一定要通過CoInitializeCoCreateInstance(此處略去了)檢查HRESULT返回...

編輯:我現在在評論中看到這確實是你的問題。雖然我會留下我的回答,以防其他人像我一樣馬虎。