HDC 保存为BMP图片步骤

	// 1. 获取DC的HBITMAP
	HBITMAP hBitmap = (HBITMAP)::GetCurrentObject(hDC, OBJ_BITMAP);
	if(0 == hBitmap)
	{
		return false;
	}
	
	// 2. 通过HBITMAP获取BITMAP
	BITMAP bitmap;
	::GetObject(hBitmap, sizeof(BITMAP), &bitmap);
	// 3. 使用BITMAP构建BITMAPINFOHEADER
	bitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
	bitmapInfoHeader.biBitCount = bitmap.bmBitsPixel;
	bitmapInfoHeader.biCompression = BI_RGB;
	bitmapInfoHeader.biPlanes = bitmap.bmPlanes;
	bitmapInfoHeader.biWidth = bitmap.bmWidth;
	bitmapInfoHeader.biHeight = bitmap.bmHeight;
	unsigned int nBytePerLine = (bitmapInfoHeader.biWidth * bitmapInfoHeader.biBitCount + 7) / 8;
	nBytePerLine = (nBytePerLine + 3) / 4 * 4;
	bitmapInfoHeader.biSizeImage = nBytePerLine * bitmapInfoHeader.biHeight;
	
	// 4. 获取颜色表
	HPALETTE hPalette = (HPALETTE)::GetCurrentObject(hDC, OBJ_PAL);
	int nEntries = 0;
	::GetObject(hPalette, sizeof(WORD), &nEntries);

	// 5. 构造BMP的颜色表RGBQUAD
	if(nEntries)
	{
		PALETTEENTRY* pPALETTEENTRY = new PALETTEENTRY[nEntries];
		::GetPaletteEntries(hPalette, 0, nEntries, pPALETTEENTRY);
		...
		delete [] pPALETTEENTRY;
	}

	// 6. 读取位图数据
	unsigned char* pData = new unsigned char[bitmapInfoHeader.biSizeImage];
	::GetBitmapBits(hBitmap, bitmapInfoHeader.biSizeImage, pData);
	...
	delete [] pData;

	// 7. 写入文件
	...


你可能感兴趣的:(图像,C/C++)