Gdi & GetDIBits的 ?T

EtherealFantasy 2010-05-01 07:20:08

BITMAP bitmap;
BITMAPINFO bitmapinfo;

HWND hwnddesktop=GetDesktopWindow();
HDC hdcdesktop=GetWindowDC(hwnddesktop);
HDC hdcmem=CreateCompatibleDC( hdcdesktop);

HBITMAP hbitmapdesktop=(HBITMAP)GetCurrentObject(hdcdesktop,OBJ_BITMAP);
GetObject(hbitmapdesktop, sizeof(BITMAP), (LPSTR)&bitmap);
bitmapinfo.bmiHeader.biPlanes=bitmap.bmPlanes; bitmapinfo.bmiHeader.biBitCount=bitmap.bmBitsPixel;
bitmapinfo.bmiHeader.biCompression =BI_RGB;
bitmapinfo.bmiHeader.biSize =sizeof(BITMAPINFOHEADER);
bitmapinfo.bmiHeader.biWidth =bitmap.bmWidth;
bitmapinfo.bmiHeader.biHeight = bitmap.bmHeight;
bitmapinfo.bmiHeader.biClrImportant=0;
bitmapinfo.bmiHeader.biClrUsed=0;
bitmapinfo.bmiHeader.biSizeImage = ((bitmapinfo.bmiHeader.biWidth *24 +31) & ~31) /8 *bitmapinfo.bmiHeader.biHeight;

HANDLE hheap=GetProcessHeap();
COLORREF* colors=(COLORREF*)HeapAlloc(hheap,HEAP_ZERO_MEMORY,bitmap.bmWidth*bitmap.bmHeight*4);

hbitmapdesktop=CreateCompatibleBitmap(hdcdesktop,bitmap.bmWidth,bitmap.bmHeight);
SelectObject(hdcmem,hbitmapdesktop);
GetDIBits(hdcmem,hbitmapdesktop,0,bitmap.bmHeight,colors,&bitmapinfo,DIB_RGB_COLORS);
… …
… …
代码如上
忙了好久 函数换了x遍 最终colors指向的内存块还是零
原来的程序 GetDIBits都正常工作
是我精神状态不好还是GDI要逗我~
崩溃边缘

还有就是 当bitmap.bmWidth和bitmap.bmHeight较大时
内存分配bitmap.bmWidth*bitmap.bmHeight*4时总是冲突 换bitmap.bmWidth*bitmap.bmHeight*5就OK了
怎么会这样 COLORREF就是4啊~
累死人了~
...全文
176 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
尹成 2010-05-01
  • 打赏
  • 举报
回复
帖子都沉底了!只能帮楼主顶到上面,以期更多高手帮楼主解决难题!
尹成 2010-05-01
  • 打赏
  • 举报
回复
bitmap.bmWidthBytes*bitmap.bmHeight
wltg2001 2010-05-01
  • 打赏
  • 举报
回复
还有就是 当bitmap.bmWidth和bitmap.bmHeight较大时
内存分配bitmap.bmWidth*bitmap.bmHeight*4时总是冲突 换bitmap.bmWidth*bitmap.bmHeight*5就OK了
===============
我记得分配内存时要考虑到行对齐,应该是:
bitmap.bmWidthBytes*bitmap.bmHeight

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧