保存bmp图片

PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp)
{
	BITMAP bmp;
	PBITMAPINFO pbmi = NULL;
	WORD    cClrBits;
	// Retrieve the bitmap color format, width, and height. 
	if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
		return pbmi;
	cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
	if (cClrBits == 1)
		cClrBits = 1;
	else if (cClrBits <= 4)
		cClrBits = 4;
	else if (cClrBits <= 8)
		cClrBits = 8;
	else if (cClrBits <= 16)
		cClrBits = 16;
	else if (cClrBits <= 24)
		cClrBits = 24;
	else cClrBits = 32;

	if (cClrBits != 24)
		pbmi = (PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER) +	sizeof(RGBQUAD) * (1 << cClrBits));
	else
		pbmi = (PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER));
	pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	pbmi->bmiHeader.biWidth = bmp.bmWidth;
	pbmi->bmiHeader.biHeight = bmp.bmHeight;
	pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
	pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
	if (cClrBits < 24)
		pbmi->bmiHeader.biClrUsed = (1 << cClrBits);
	// If the bitmap is not compressed, set the BI_RGB flag. 
	pbmi->bmiHeader.biCompression = BI_RGB;
	pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits + 31) & ~31) / 8
		* pbmi->bmiHeader.biHeight;
	pbmi->bmiHeader.biClrImportant = 0;
	return pbmi;
}


void saveImage(unsigned char*data,unsigned int size)
{

	std::ofstream fout("ieshot.bmp", std::ios::out | std::ios::binary);
	if (fout.is_open())
	{
		fout.write((char*)data,size);
	}
}
BOOL CreateBMPBuffer(PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC)
{
	BITMAPFILEHEADER hdr;       // bitmap file-header 
	LPBYTE lpBits =  new BYTE[pbi->bmiHeader.biSizeImage];              // memory pointer 
	if (!GetDIBits(hDC, hBMP, 0, (WORD)pbi->bmiHeader.biHeight, lpBits, pbi,
		DIB_RGB_COLORS))
	{	
		LocalFree((HLOCAL)pbi);
		delete [] lpBits;
		return FALSE;
	}
	hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M" 
	// Compute the size of the entire file. 
	hdr.bfSize = (DWORD)(sizeof(BITMAPFILEHEADER) +
		pbi->bmiHeader.biSize + pbi->bmiHeader.biClrUsed
		* sizeof(RGBQUAD) + pbi->bmiHeader.biSizeImage);
	hdr.bfReserved1 = 0;
	hdr.bfReserved2 = 0;
	// Compute the offset to the array of color indices. 
	hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +pbi->bmiHeader.biSize + pbi->bmiHeader.biClrUsed* sizeof(RGBQUAD); 
	int  blength = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +  pbi->bmiHeader.biSizeImage;
	BYTE* buffer = new BYTE[blength];
	 memcpy( buffer, &hdr, sizeof(BITMAPFILEHEADER));
	 memcpy( buffer+sizeof(BITMAPFILEHEADER), &pbi->bmiHeader, sizeof(BITMAPINFOHEADER));
	 memcpy( buffer+(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)),lpBits, pbi->bmiHeader.biSizeImage);
	if(g_bSaveImage)
		saveImage(buffer,blength);
	 delete [] lpBits;
	LocalFree((HLOCAL)pbi);	
	delete[] buffer;	
	return FALSE;

}

将BMP保存到文件中或者直接用BMP内存创建纹理,适用于窗口截图,并将窗口图片纹理粘贴到模型上,使用d3d创建纹理的接口即可做到,如:

 if( FAILED( D3DXCreateTextureFromFileInMemory( g_pd3dDevice, buffer,blength, &g_pTexture ) ) )

之后再将g_pTexture 绑定到模型上


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值