#include#include using namespace std; void main() { ifstream infile("bm.bmp", ios::in | ios::binary); BITMAPFILEHEADER bmpHeader; // 获取文件大小 infile.seekg(0, ios::end); long nfilelen = infile.tellg(); infile.seekg(0, ios::beg); // 读取文件头 infile.read((char*)&bmpHeader, sizeof(bmpHeader)); if (bmpHeader.bfType != 0x4d42) return ; // 占用一块内存 HGLOBAL hDIB = GlobalAlloc(GMEM_FIXED, nfilelen); if (hDIB == 0) return ; // 把文件写入DIB infile.read((char*)hDIB, nfilelen - sizeof(BITMAPFILEHEADER)); // 锁定内存, 位图信息指向该内存 // BITMAPINFO == 位图信息头 + 调色板 BITMAPINFO &bmpInfo = *(BITMAPINFO*)GlobalLock(hDIB); // 位图信息头指向该内存 // BITMAPINFOHEADER *pInfoHead = (BITMAPINFOHEADER*)hDIB; // 位图信息头大小 // int nHeadSize = sizeof(BITMAPINFOHEADER); // 获取颜色数组 BYTE *pBmpPixelBuffer=(BYTE *)&bmpInfo + bmpHeader.bfOffBits - sizeof(BITMAPFILEHEADER); HDC hDC = GetDC(NULL); HBITMAP hBitmap; hBitmap=CreateDIBitmap(hDC,(BITMAPINFOHEADER *)&bmpInfo, CBM_INIT,(VOID *)pBmpPixelBuffer,&bmpInfo,DIB_RGB_COLORS); HDC memDC; memDC = CreateCompatibleDC(hDC); SelectObject(memDC, hBitmap); BitBlt(hDC, 0, 0, 500, 500, memDC, 0, 0, SRCCOPY); DeleteDC(memDC); GlobalFree(hDIB); infile.close(); }
http://blog.csdn.net/mmc1206x/article/details/8686822