平台:
windows mobile6.5(应该也适用Windows CE),VS2008
问题:
位图资源中已经有个叫IDB_BITMAP1的位图,想获得它的像素数据(即unsigned char类型的一块内存数据),
在PC上的方法:
m_bitmap.LoadBitmap(IDB_BITMAP1); // 加载位图资源, m_bitmap为CBitmap类型
BITMAP bmStruct;
m_bitmap.GetBitmap(&bmStruct);
// bmStruct.bmWidth和bmStruct.bmHeight 为位图资源的尺寸
// bmStruct.bmBits 为像素数据块
但是:在windows mobile平台上可以获得bmStruct.bmWidth和bmStruct.bmHeight 的正确值,但是bmStruct.bmBits始终为NULL!
解决方法:使用FindResource(),LoadResource()等API
m_pBoxData = NULL; // unsigned char* unsigned char *dataBuf = NULL; // total memory block, including image header DWORD dwBufSize = 0; // size of total memory block HRSRC hrc = FindResource(AfxGetResourceHandle(),MAKEINTRESOURCE(IDB_BITMAP3),RT_BITMAP); HGLOBAL hGlobal = LoadResource(NULL,hrc); dwBufSize = ::SizeofResource(NULL,hrc); // BITMAP(imagedata+inforhead(40bytes,fixed)) dataBuf = (PBYTE)::LockResource(hGlobal); // imagedata pointer m_pBoxData = new unsigned char[dwBufSize-40];// -40bytes(imageheader) memcpy(m_pBoxData, dataBuf, sizeof(unsigned char)*(dwBufSize-40)); // copy FreeResource(hGlobal); BITMAP tmpBM; m_bitmapBox.GetBitmap(&tmpBM);// obtain the width and height of the bmp image m_bmpImageWidth = tmpBM.bmWidth; m_bmpImageHeight = tmpBM.bmHeight;
参考:http://hi.baidu.com/gengyit/blog/item/5785a5fe8181f4f1fc037f91.html#0