保存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 绑定到模型上


你可能感兴趣的:(图片处理,D3D)