【发布时间】:2014-07-03 08:21:18
【问题描述】:
我有一个存储为 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,这听起来像是我想要的:
SetDIBits 函数使用指定 DIB 中的颜色数据设置兼容位图 (DDB) 中的像素。
在我的例子中,DIB 是 32bpp RGBA 阵列,而 DDB 是 dispBMP,我使用 CreateCompatibleBitmap 创建。
所以我没有打电话给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是真的吗?