获取HBITMAP,导出带透明度的png图片

本文介绍如何从HBITMAP中获取数据,并保存为包含透明度信息的PNG图像。通过转换和处理位图数据,可以确保导出的PNG文件正确保留了原有的透明效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 从HDC 获取 HBitmap
	STDMETHOD(ExportHBitmap)(ULONG_PTR* o_hBitmap)
	{
		HBITMAP hBmp = NULL;
		HANDLE hOld;
		HDC hMemDC = NULL;

		long lRectPara[4];
		GetWndRect(lRectPara);
		long nWidth = lRectPara[2];
		long nHeight = lRectPara[3];

		hMemDC = CreateCompatibleDC(m_hDC);

		BITMAPINFO bi;
		ZeroMemory(&bi, sizeof(BITMAPINFO));

		// 获得颜色深度
		int iBitsPerPixel = CDC::FromHandle(m_hDC)->GetDeviceCaps(BITSPIXEL);

		// 设置每个颜色占位数
		if (iBitsPerPixel <= 8)
		{
			iBitsPerPixel = 8;
		}
		else if (iBitsPerPixel <= 16)
		{
			iBitsPerPixel = 16;
		}
		else if (iBitsPerPixel <= 24)
		{
			iBitsPerPixel = 24;
		}
		else
		{
			iBitsPerPixel = 32;
		}		

		// 设置DIB位图需要的BITMAPINFO结构
		bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		bi.bmiHeader.biPlanes = 1;
		bi.bmiHeader.biBitCount = iBitsPerPixel;
		bi.bmiHeader.biWidth = nWidth;
		bi.bmiHeader.biHeight = -nHeight;
		bi.bmiHeader.biSizeImag
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值