在一个很大的快捷方式中,我正在尝试编写一个程序,它通过从另一个应用程序接收到的NamedPipe位图来绘制位图。
我分别发送BITMAPINFO结构和位图位,它们是我从GetBitmapBits函数获得的。在接收端,我有一个线程,它使用HeapAlloc覆盖两个全局指针: pbmi_paint (指向BITMAPINFO)和lpBitmapBits_paint (用于位图位)。pbmi_paint中的数据似乎总是合法的。然后,我尝试在WndProc中使用以下代码绘制此代码:
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
if(pbmi_paint != NULL && lpBitmapBits_paint != NULL) {
hBitmap = CreateDIBitmap( hdc,&(pbmi_paint->bmiHeader), CBM_INIT,(LPVOID) lpBitmapBits_paint,pbmi_paint,DIB_RGB_COLORS);
GetObject(hBitmap, sizeof(bitmap), &bitmap);
hdcMem = CreateCompatibleDC(hdc);
oldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap);
BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBitmap);
DeleteDC(hdcMem);
DeleteObject(hBitmap);
}
EndPaint(hWnd, &ps);然后我什么也得不到(黑色矩形)。我检查了bitmap.bmBits,它等于0x00000000 (NULL,位图结构的另一个成员设置正确)。我还尝试通过以下方式手动设置bitmap.bmBits:
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
if(pbmi_paint != NULL && lpBitmapBits_paint != NULL) {
hBitmap = CreateDIBitmap( hdc,&(pbmi_paint->bmiHeader), CBM_INIT,(LPVOID) lpBitmapBits_paint,pbmi_paint,DIB_RGB_COLORS);
GetObject(hBitmap, sizeof(bitmap), &bitmap);
bitmap.bmBits = lpBitmapBits_paint;
hBitmap1 = CreateBitmapIndirect(&bitmap);
hdcMem = CreateCompatibleDC(hdc);
oldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap1);
BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBitmap);
DeleteDC(hdcMem);
DeleteObject(hBitmap);
DeleteObject(hBitmap1);
}在此之后,hBitmap1为NULL。所以我的问题是为什么我不能在CreateDIBitmap或CreateBitmapIndirect中使用指向数据的指针?这些函数会检查数据是否有效吗?
发布于 2013-01-28 03:45:58
位图在Windows中是一团糟,尤其是DDB (设备相关位图)。如果可能的话,我总是更喜欢使用DIB(设备无关位图)。它们更容易使用,而且在现代硬件中应该不会有任何明显的性能差异。
DIB是使用函数CreateDIBSection()创建的,像素数据是独立完成的。如下所示:
void *pixels;
HBITMAP hBmp = CreateDIBSection(NULL, pbmi_paint, DIB_RGB_COLORS, &pixels, NULL, 0);
memcpy(pixels, lpBitmapBits_paint, NumBytesInBitmapBits_paint);https://stackoverflow.com/questions/14549123
复制相似问题