网语飘飘.Net/Delphi攻坚战

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
将DIB位图写入位图文件

如果我们有一个DIB位图句柄,将它写入位图文件非常简单。你只需根据位图中
的内容填写结构BITMAPINFOHEADER。主要是将该结构中的三个成员设置,将bfType
设为"BM",另外设置bfSize和bfOffBits。

如果你想将DDB位图存为位图文件,只需将DDB转为DIB后使用本方法即可。

// 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;
}
posted on 2006-06-18 20:37  网语飘飘  阅读(399)  评论(0)    收藏  举报