BOOL SaveImageDataToBMP( LPCTSTR lpFileName, DWORD width, DWORD height, DWORD bits, void* pData ) { if( bits != 24 && bits != 32 ) return FALSE; BITMAPFILEHEADER bfh; ZeroMemory( &bfh, sizeof(BITMAPFILEHEADER) ); bfh.bfType = ((WORD)('M'<<8)|'B'); bfh.bfSize = 0; bfh.bfReserved2 = 0; bfh.bfReserved1 = 0; bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); BITMAPINFO BitmapInfo; BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); BitmapInfo.bmiHeader.biPlanes = 1; BitmapInfo.bmiHeader.biBitCount = bits; BitmapInfo.bmiHeader.biCompression = BI_RGB; BitmapInfo.bmiHeader.biSizeImage = 0; BitmapInfo.bmiHeader.biXPelsPerMeter = 0; BitmapInfo.bmiHeader.biYPelsPerMeter = 0; BitmapInfo.bmiHeader.biClrUsed = 0; BitmapInfo.bmiHeader.biClrImportant = 0; BitmapInfo.bmiHeader.biWidth = width; BitmapInfo.bmiHeader.biHeight = height; int dwPitch = width * bits / 8; while( dwPitch % 4 != 0 ) dwPitch++; FILE* pFile = _tfopen( lpFileName, _T("wb") ); fwrite( &bfh, sizeof(BITMAPFILEHEADER), 1, pFile ); fwrite( &BitmapInfo, sizeof(BITMAPINFOHEADER), 1, pFile ); fwrite( pData, sizeof(char), height * dwPitch, pFile ); fclose( pFile ); return TRUE; }
转载于:https://www.cnblogs.com/LinuxHunter/archive/2010/11/23/1885841.html