我将位图存储为 BGRA 字节数组。这是我用来绘制位图的代码:
CDC *dispDC = new CDC();
dispDC->CreateCompatibleDC(pDC);
CBitmap *dispBMP = new CBitmap();
dispBMP->CreateCompatibleBitmap(pDC, sourceImage->GetWidth(), sourceImage->GetHeight());
dispDC->SelectObject(this->dispBMP);
translatedImage
数组中像素的实际复制是这样发生的:
dispBMP->SetBitmapBits(sourceImage->GetArea() * 4, translatedImage);
然后经过更多处理后,我调用 pDC->StretchBlt
并将 dispDC
作为源 CDC。这在本地登录时工作正常,因为显示也设置为 32bpp。
我使用远程桌面登录后,显示变为 16bpp,图像被损坏。罪魁祸首是 SetBitmapBits
;即,为了使其正常工作,我必须使用我想要显示的 16bpp 版本正确填充 translatedImage
。我没有自己做,而是搜索了文档并找到了 SetDIBits
,这听起来像是我想要的:
The SetDIBits function sets the pixels in a compatible bitmap (DDB) using the color data found in the specified DIB.
在我的例子中,DIB 是 32bpp RGBA 数组,DDB 是我用 CreateCompatibleBitmap
创建的 dispBMP
。
所以我没有调用 SetBitmapBits
,而是这样做的:
BITMAPINFO info;
ZeroMemory(&info, sizeof(BITMAPINFO));
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biSizeImage = sourceImage->GetArea()*4;
info.bmiHeader.biWidth = sourceImage->GetWidth();
info.bmiHeader.biHeight = sourceImage->GetHeight();
info.bmiHeader.biClrUsed = 0;
int r = SetDIBits(pDC->GetSafeHdc(), (HBITMAP)dispBMP,
0, sourceImage->GetHeight(), translatedImage,
&info, DIB_PAL_COLORS);
但是,r
始终为零,自然地,我的窗口中只有黑色。代码有什么问题?
最佳答案
根据documentation for SetDIBits
:
The bitmap identified by the hbmp parameter must not be selected into a device context when the application calls this function.
在您的示例代码中,您在创建它之后将其选择到设备上下文中,所以大概这就是 SetDIBits
失败的原因。
关于c++ - MFC BitBlt 和 SetDIBits 与 SetBitmapBits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24548520/