GetDIBits获取DIB数据(把原数据反转一下)

#define LINE_BYTES(w, b) (((w) * (b) + 31)/32*4)

case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...

BITMAPINFO bmpinfo = {0};
bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); //必须,初始化bmpinfo.bmiHeader中的第一个参数

HBITMAP hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
if(NULL == hBitmap)
MessageBoxA(hWnd, "LoadBitmap error", "WM_PAINT", MB_OK);
// HDC hdc = GetDC(hWnd);
int scanline = GetDIBits(hdc, hBitmap, 0, 0, NULL, &bmpinfo, DIB_RGB_COLORS);//初始化bmpinfo.bmiHeader中第二到第四3个参数
if(0 == scanline) //windows API
MessageBoxA(hWnd, "GetDIBits error", "WM_PAINT", MB_OK);

bmpinfo.bmiHeader.biCompression = BI_RGB; //必须
bmpinfo.bmiHeader.biPlanes = 1; //必须
// bmpinfo.bmiHeader.biXPelsPerMeter = 10000; //可以有可以木有
// bmpinfo.bmiHeader.biYPelsPerMeter = 10000; //可以有可以木有
// bmpinfo.bmiHeader.biSizeImage = 0; //可以有可以木有
//总共初始化bmpinfo.bmiHeader中前六个参数,接下来可以利用初始化过得bmpinfo结构将位图数据copy进大小分配好的内存中

char buff[MAX_PATH];
sprintf(buff, "width: %d/nheight: %d/nbiBitCount: %d/nXPerMeter: %d/nYPerMeter: %d", /
bmpinfo.bmiHeader.biWidth, bmpinfo.bmiHeader.biHeight, bmpinfo.bmiHeader.biBitCount, /
bmpinfo.bmiHeader.biXPelsPerMeter, bmpinfo.bmiHeader.biYPelsPerMeter);
// MessageBoxA(hWnd, buff, "WM_PAINT", MB_OK);

void* pdata = malloc(LINE_BYTES(bmpinfo.bmiHeader.biWidth,bmpinfo.bmiHeader.biBitCount)*bmpinfo.bmiHeader.biHeight);
if(pdata == NULL)
{
MessageBoxA(hWnd, "Malloc Memory Error!", "WM_PAINT", MB_OK);
return 0;
}
scanline = GetDIBits(hdc, hBitmap, 0, bmpinfo.bmiHeader.biHeight, (LPVOID)pdata, &bmpinfo, DIB_RGB_COLORS);
if(0 == scanline) //windows API
MessageBoxA(hWnd, "GetDIBits error2", "WM_PAINT", MB_OK);

sprintf(buff, "scanline: %d", scanline);
// MessageBoxA(hWnd, buff, "WM_PAINT", MB_OK);

sprintf(buff, "width: %d/nheight: %d/nbiBitCount: %d/nXPerMeter: %d/nYPerMeter: %d", /
bmpinfo.bmiHeader.biWidth, bmpinfo.bmiHeader.biHeight, bmpinfo.bmiHeader.biBitCount, /
bmpinfo.bmiHeader.biXPelsPerMeter, bmpinfo.bmiHeader.biYPelsPerMeter);
// MessageBoxA(hWnd, buff, "WM_PAINT", MB_OK);

::SetDIBitsToDevice(hdc, 0, 0,bmpinfo.bmiHeader.biWidth, /
bmpinfo.bmiHeader.biHeight, 0, 0, 0, bmpinfo.bmiHeader.biHeight, /
pdata, (BITMAPINFO*)&bmpinfo, DIB_RGB_COLORS);

free(pdata);

EndPaint(hWnd, &ps);
}
break;

posted @ 2013-11-30 11:34  高_山_流_水  阅读(647)  评论(0)    收藏  举报