簡體   English   中英

來自std :: vector中原始RGB值的CBitmap <std::uint32_t> 拿着陣列

[英]CBitmap from raw RGB values in std::vector<std::uint32_t> holding array

我在網上找到的庫中的以下代碼中有一個原始RGBA值的位圖。 “svgren。

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值加載到位圖中。 有什么想法嗎?

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

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

矩形中的每條掃描線必須是字1對齊的(非字對齊的掃描線必須用零填充)。

根據假設,內存已正確對齊,並將緩沖區重新解釋為指向字節的指針已明確定義,這是一個具有適當資源處理的實現:

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成為新創建的位圖的唯一所有者。


1 我相信這應該讀dword對齊

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM