win32 解析、显示webp图片,及位图透明

win32 解析、显示webp图片,及位图透明

cpp 复制代码
CImage img;

#include <webp/decode.h>
#include <fstream>
#include <wingdi.h>

void parse(CDC* dc)
{
	std::ifstream ifs("None-1.webp", std::ios::binary);
	ifs.seekg(0, std::ios::end);
	auto size = ifs.tellg();
	ifs.seekg(0, std::ios::beg);

	std::vector<uint8_t> data(size);
	ifs.read((char*)data.data(), data.size());
	ifs.close();

	int width = 0;
	int height = 0;
	auto ret = WebPGetInfo(data.data(), size, &width, &height);

	//WebPDecoderConfig config;
#if 0
	auto webpdata = WebPDecodeRGBA(data.data(), size, &width, &height);
	for (int of = 0, max = width * height * 4; of < max; of += 4) {
		auto b = webpdata[of + 0];
		auto g = webpdata[of + 1];
		auto r = webpdata[of + 2];
		webpdata[of + 0] = r;
		webpdata[of + 1] = g;
		webpdata[of + 2] = b;
		//webpdata[of+0] = 255; // blue
		//webpdata[of+1] = 255; // green
		//webpdata[of+2] = 255; // red
		webpdata[of+3] = 0;
	}
#else 
	auto webpdata = WebPDecodeBGRA(data.data(), size, &width, &height);
#endif

	std::cout << "width: " << width << ", height: " << height << std::endl;

	uint32_t nPlanes = 1;
	uint32_t nBitCount = 4 * 8;
	auto cj = (((width * nPlanes * nBitCount + 15) >> 4) << 1) * height;
	std::cout << "buffer size: " << cj << std::endl;

	auto hbitmap = CreateBitmap(width, height, nPlanes, nBitCount, webpdata);
	//uint32_t errno_ = ERROR_INVALID_BITMAP;
	std::cout << "create bitmap: " << hbitmap << std::endl;

	CDC memDC;						//定义一个显示设备对象
	memDC.CreateCompatibleDC(dc);			//创建CDC兼容设备
	memDC.SetBkMode(TRANSPARENT);
	memDC.SelectObject(hbitmap);					//设备选择当前的图纸-位图

	DeleteObject(hbitmap);

	dc->SetStretchBltMode(STRETCH_HALFTONE);
	dc->StretchBlt(100, 100, 100, 100, &memDC, 0, 0, width, height, SRCAND);
	//dc->BitBlt(100, 100, width, height, &memDC, 0, 0, SRCAND);
	//dc->TransparentBlt(100, 100, width, height, &memDC, 0, 0, width, height, 0x00ffff00);
}
相关推荐
charlie1145141911 天前
通用GUI编程技术——图形渲染实战(二十八)——图像格式与编解码:PNG/JPEG全掌握
开发语言·c++·windows·学习·图形渲染·win32
charlie1145141912 天前
通用GUI编程技术——图形渲染实战(二十七)——坐标变换与矩阵:三级坐标系
c++·学习·c·图形渲染·win32
charlie1145141912 天前
通用GUI编程技术——图形渲染实战(二十六)——GDI+与GDI架构差异:抗锯齿与渐变
c++·windows·学习·图形渲染·win32
charlie1145141913 天前
通用GUI编程技术——图形渲染实战(二十五)——Alpha混合与透明效果:分层窗口实战
c++·windows·学习·图形渲染·win32
charlie1145141913 天前
通用GUI编程技术——图形渲染实战(二十四)——GDI Region与裁切:不规则窗口与可视化控制
c++·windows·学习·c·图形渲染·win32
charlie1145141917 天前
通用GUI编程技术——Win32 原生编程实战(二十二)——GDI 位图操作:BitBlt、StretchBlt 与图像处理
c++·windows·学习·c·win32
Peter(阿斯拉)13 天前
[WTL/Win32]_[初级]_[如何在工作线程计算文本的宽高]
win32·gdi·wtl·gdiplus·内存hdc
charlie11451419115 天前
通用GUI编程技术——Win32 原生编程实战(十八)——GDI 设备上下文(HDC)完全指南
开发语言·c++·ide·学习·visual studio·win32
charlie11451419116 天前
通用GUI编程技术——Win32 原生编程实战(十六)——Visual Studio 资源编辑器使用指南
开发语言·c++·ide·学习·gui·visual studio·win32
charlie1145141911 个月前
通用GUI编程技术——Win32 原生编程实战(五)——ListView 控件详解
windows·学习·gui·win32·编程指南