我有一个以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);
然后在进一步处理后,我使用
dispDC
作为源CDC调用pDC->StretchBlt
。当本地登录时,这个方法可以正常工作,因为显示器的设置也是32bpp。一旦使用远程桌面登录,显示器就会变成16bpp,图像就会失真。问题出在
SetBitmapBits
上;即为了让它正常工作,我必须正确地填充translatedImage
,使其成为我想要显示的16bpp版本。而不是自己动手做这件事,我查阅了文档并发现了SetDIBits
,它似乎可以实现我的需求:“SetDIBits函数使用指定DIB中找到的颜色数据来设置兼容位图(DDB)中的像素。”
在我的情况下,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
总是零,自然地,我的窗口里什么也看不到,都是黑色的。这段代码有问题吗?
SetDIBits
的输入?例如,在这种情况下,dispBMP!=NULL
是否为真? - Ilya