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);
}
相关推荐
!win !7 天前
WebP图片使用踩坑
小程序·uniapp·webp
Flame_Cyclone2 个月前
Win32获取系统版本信息
c++·win32·windows版本信息
Flame_Cyclone3 个月前
WinHttp异步请求封装
c++·win32·winhttp
△曉風殘月〆3 个月前
Win32打开UWP应用
vc++·uwp·win32
Struggle_zhu3 个月前
浏览器是否支持webp
webp
△曉風殘月〆3 个月前
WPF颜色(SolidColorBrush)和Win32颜色(COLOREF)互转的方法
wpf·win32·solidcolorbrush·colorref
Flame_Cyclone3 个月前
FakerInput 键盘鼠标输入封装
c++·windows·win32·fakerinput
Flame_Cyclone3 个月前
编写XBOX控制器实现鼠标键盘输入
c++·windows·win32·xbox·控制器模拟键盘鼠标
Flame_Cyclone3 个月前
原始输入解析XBOX手柄
c++·windows·win32·xbox