将24位及32位图像数据保存到位图中

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值