函数原型
int GetDIBits(
HDC hdc, // handle to DC
HBITMAP hbmp, // handle to bitmap
UINT uStartScan, // first scan line to set
UINT cScanLines, // number of scan lines to copy
LPVOID lpvBits, // array for bitmap bits
LPBITMAPINFO lpbi, // bitmap data buffer
UINT uUsage // RGB or palette index
);
参数及返回值功能略,详见msdn.
#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;