+-
c – MFC BitBlt和SetDIBits与SetBitmapBits
我有一个位图存储为BGRA字节数组.这是我用来绘制位图的代码:

CDC *dispDC = new CDC();
dispDC->CreateCompatibleDC(pDC);
CBitmap *dispBMP = new CBitmap();
dispBMP->CreateCompatibleBitmap(pDC, sourceImage->GetWidth(), sourceImage->GetHeight());
dispDC->SelectObject(this->dispBMP);

translateImage数组中像素的实际复制发生在:

dispBMP->SetBitmapBits(sourceImage->GetArea() * 4, translatedImage);

然后在一些更多处理之后,我将pDC-> StretchBlt称为dispDC作为源CDC.这在本地登录时工作正常,因为显示也设置为32bpp.

一旦我使用远程桌面登录,显示屏将变为16bpp并且图像被破坏.罪魁祸首是SetBitmapBits;即为了它的工作,我必须正确填写translateImage与我想要显示的16bpp版本.我没有自己这样做,而是搜索了文档,发现了听起来像我想要的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 - 乐贴网