如何将CBitmap写入文件

 如何将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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值