2016-09-13 66 views
0

我在網上找到一個庫中的以下代碼中的原始RGBA值中有一個位圖。 「svgren。從std :: vector中的原始RGB值的CBitmap <std :: uint32_t>控制數組

auto img = svgren::render(*dom, width, height); //uses 96 dpi by default 
//At this point the 'width' and 'height' variables were filled with 
//the actual width and height of the rendered image. 
//Returned 'img' is a std::vector<std::uint32_t> holding array of RGBA values. 

我需要知道如何讓這幅畫進入的CBitmap這樣我就可以在一個MFC圖片控件中顯示它。我的前膠料它,我知道如何顯示在控制位圖什麼我不能做的是RGBA值加載到該位圖。任何想法嗎?

+0

直接使用createBitmap: –

+0

CBitmap Chb; \t \t \t HBITMAP bmp = CreateBitmap(width,height,1,32,&* img.begin()); \t \t \t ASSERT_ALWAYS(BMP!= NULL) \t \t \t Chb.Attach(BMP); \t \t \t //PicControl.ModifyStyle(0xF,SS_BITMAP,SWP_NOSIZE); \t \t \t //PicControl.SetBitmap(Chb); \t \t \t mProjectorWindow.m_picControl.ModifyStyle(0xF,SS_BITMAP,SWP_NOSIZE); \t \t \t mProjectorWindow.m_picControl.SetBitmap(Chb); –

+0

相關 - https://stackoverflow.com/questions/4993518/arraybyte-to-hbitmap-or-cbitmap – sashoalm

回答

0
CBitmap Chb; 
HBITMAP bmp = CreateBitmap(width, height, 1, 32, &*img.begin()); 
ASSERT_ALWAYS(bmp != NULL) 
Chb.Attach(bmp); 
//PicControl.ModifyStyle(0xF, SS_BITMAP, SWP_NOSIZE); 
//PicControl.SetBitmap(Chb); 
mProjectorWindow.m_picControl.ModifyStyle(0xF, SS_BITMAP, SWP_NOSIZE); 
mProjectorWindow.m_picControl.SetBitmap(Chb); 
+0

你應該可以使用'img.data()'而不是'&* img.begin()'(假設這是'std :: vector') –

+0

正如您在[相關問題](http:// stackoverflow。com/q/39596419/1889329),該解決方案既包含GDI資源泄漏又包含懸掛指針。 – IInspectable

1

CBitmap::CreateBitmap成員函數可以從內存塊構造一個位圖。該LP位元參數需要一個指向字節 values。將指針傳遞給一個數組uint32_t值在技術上是未定義的行爲(儘管它將適用於Windows的所有小端實現)。

必須特別注意內存佈局。這僅記錄在Windows API調用CreateBitmap,而不是在所有在場的MFC文檔中:

在矩形每條掃描線必須是字對齊(掃描線未字對齊必須補齊用零)。

基於這樣的假設,即內存被正確對齊,並重新詮釋被很好地定義了緩衝區指針字節,這裏是用適當的資源處理的實現:

CBitmap Chb; 
Chb.CreateBitmap(width, height, 1, 32, img.data()); 
mProjectorWindow.m_picControl.ModifyStyle(0xF, SS_BITMAP, SWP_NOSIZE); 
Chb.Attach(mProjectorWindow.m_picControl.SetBitmap(Chb.Detach())); 

的最後一行代碼在m_picControlChb之間交換GDI資源的所有權。這可確保正確清理以前由m_picControl擁有的GDI資源,並使m_picControl成爲新創建的位圖的唯一所有者。


我相信這should read dword aligned

+0

Chm從CreateBitmap獲取位圖。但是,那和SetBitmap之間似乎沒有聯繫?我認爲Chm是一個成員變量CBitmap?還是Chb的拼寫錯誤? –

相關問題