1. 程式人生 > >從位圖數據取得位圖句柄

從位圖數據取得位圖句柄

int cto mes long eat bits pix != bitmap

#include <windows.h>  
#include <fstream>  
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

從位圖數據取得位圖句柄