2016-09-13 64 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的拼写错误? –

相关问题