如何将CBitmap写入文件 如果具有一个设备无关的位图句柄,把一个位图写入BMP文件非常简单:在位图内容之 后写入BITMAPINFOHEADER信息即可。需要设置BITMAPINFOHEADER的三个成员是bfType, 其值为"BM",bfSize,其值是位图的大小,以及,bfOffBits,其值为文件开始到位图 位的偏移量。 // WriteDIB - Writes a DIB to file // Returns - TRUE on success // szFile - Name of file to write to // hDIB - Handle of the DIB BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB) { BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi; if (!hDIB) return FALSE; CFile file; if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) ) return FALSE; lpbi = (LPBITMAPINFOHEADER)hDIB; int nColors = 1 << lpbi->biBitCount; // Fill in the fields of the file header hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM" hdr.bfSize="GlobalSize" (hDIB) + sizeof( hdr ); hdr.bfReserved1="0;" hdr.bfReserved2="0;" hdr.bfOffBits="(DWORD)" (sizeof( hdr ) + lpbi->biSize + nColors * sizeof(RGBQUAD)); // Write the file header file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits file.Write( lpbi, GlobalSize(hDIB) ); return TRUE; }
如何将CBitmap写入文件
最新推荐文章于 2022-08-03 16:19:36 发布