天天看點

VC++ HBITMAP儲存檔案

請先看下面這段代碼:

BOOL SaveBmp(HBITMAP bmp, string path)
{
	FILE* f = fopen(path.c_str(), "wb");
	if (!f)
		return 0;

	BITMAP bmpinfo;
	GetObject(bmp, sizeof(BITMAP), &bmpinfo);
	DWORD dwBmBitsSize = ((bmpinfo.bmWidth * 32 + 31) / 32) * 3 * bmpinfo.bmHeight;

	BITMAPFILEHEADER bf;
	bf.bfType = 0x4D42;
	bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;
	bf.bfReserved1 = 0;
	bf.bfReserved2 = 0;
	bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

	BITMAPINFOHEADER bi;
	bi.biSize = sizeof(BITMAPINFOHEADER);
	bi.biWidth = bmpinfo.bmWidth;
	bi.biHeight = bmpinfo.bmHeight;
	bi.biPlanes = 1;
	bi.biBitCount = 24;
	bi.biCompression = BI_RGB;
	bi.biSizeImage = 0;
	bi.biXPelsPerMeter = 0;
	bi.biYPelsPerMeter = 0;
	bi.biClrUsed = 8;
	bi.biClrImportant = 0;
	
	HDC dc = ::GetDC(NULL);
	BYTE* lpvBits = new BYTE[dwBmBitsSize];
	m