The CBitmap::CreateBitmap member function can construct a bitmap from a block of memory. The lpBits argument expects a pointer to byte values. Passing a pointer to an array of uint32_t
values is technically undefined behavior (although it will work on all little-endian implementations of Windows).
Special care must be taken for the memory layout. This is only documented for the Windows API call CreateBitmap and not at all present in the MFC documentation:
Each scan line in the rectangle must be word1 aligned (scan lines that are not word aligned must be padded with zeros).
Based on the assumption, that the memory is properly aligned, and reinterpreting the buffer as a pointer to bytes is well defined, here's an implementation with proper resource handling:
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()));
The last line of code exchanges ownership of the GDI resource between the m_picControl
and Chb
. This ensures proper cleanup of the GDI resource previously owned by the m_picControl
, and makes the m_picControl
the only owner of the newly created bitmap.
1 I believe this should read dword aligned.