如何将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写入文件
最新推荐文章于 2020-10-28 15:25:17 发布
博客介绍了将CBitmap写入BMP文件的方法,若有设备无关的位图句柄,在位图内容后写入BITMAPINFOHEADER信息即可。还给出了WriteDIB函数的代码示例,包含文件头设置和写入操作,以实现位图写入文件。
1479

被折叠的 条评论
为什么被折叠?



