[SDK]创建DC,保存DC为BMP文件

HDC CreateBitmapDC(LONG Width, LONG Height)
{
	HDC hDC, hMemDC;
	BITMAPINFO bi;
	HBITMAP hBitmap;
	PVOID Bits;

	hDC = NULL;
	hMemDC = CreateCompatibleDC(NULL);
	memset(&bi, 0, sizeof(bi));
	bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bi.bmiHeader.biWidth = Width;
	bi.bmiHeader.biHeight = Height;
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biBitCount = 24;
	if (hBitmap = CreateDIBSection(hMemDC, &bi, DIB_RGB_COLORS, &Bits, NULL, 0))
	{
		DeleteObject(SelectObject(hMemDC, hBitmap));
		hDC = hMemDC;
	}
	else
	{
		DeleteDC(hMemDC);
	}
	return hDC;// use DeleteDC to free handle
}

BOOL SaveDCToFile(HDC hDc, TCHAR *lpFile)
{
	BOOL bRet;

	bRet = FALSE;
	if (HBITMAP hBitmap = (HBITMAP)GetCurrentObject(hDc, OBJ_BITMAP))
	{
		BITMAP bm;

		if (GetObject(hBitmap, sizeof(bm), &bm) != 0)
		{
			BITMAPFILEHEADER bfh;
			BITMAPINFOHEADER bih;
			HANDLE hFile;

			memset(&bfh, 0, sizeof(bfh));
			memset(&bih, 0, sizeof(bih));
			bfh.bfType = 0x4D42; // "BM"
			bfh.bfSize = 0;
			bfh.bfOffBits = sizeof(bfh) + sizeof(bih);

			bih.biSize = sizeof(bih);
			bih.biWidth = bm.bmWidth;
			bih.biHeight = bm.bmHeight;
			bih.biPlanes = bm.bmPlanes;
			bih.biBitCount = bm.bmBitsPixel;
			bih.biCompression = BI_RGB;
			
			hFile = CreateFile(lpFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
			if (hFile != INVALID_HANDLE_VALUE)
			{
				DWORD dwOpt;

				if (WriteFile(hFile, &bfh, sizeof(bfh), &dwOpt, NULL)
					&& WriteFile(hFile, &bih, sizeof(bih), &dwOpt, NULL)
					&& WriteFile(hFile, bm.bmBits, bm.bmWidthBytes * bm.bmHeight, &dwOpt, NULL))
				{
					bRet = TRUE;
				}
				CloseHandle(hFile);
			}
		}
	}
	return bRet;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值