Easyx图形库函数功能扩展(2)-显示透明位图

函数功能说明:

使用`TransparentBlt`函数绘制带透明通道的png图片。
void PutImagePng(IMAGE* dstImg,int x, int y,int w,int h, IMAGE* srcImg,UINT transparentcolor)
{
	HDC dstDC = GetImageHDC(dstImg);
	HDC srcDC = GetImageHDC(srcImg);
	int width = srcImg->getwidth();
	int height = srcImg->getheight();
	//使用windows GDI函数实现透明位图
	TransparentBlt(dstDC, x, y, (w==0 && h==0)?width:w, (w==0 && h==0)?height:h, srcDC, 0, 0, width, height, transparentcolor);
}

参数说明

1.使用前需加载动态库 #pragma comment(lib,"MSIMG32.LIB")
2.dstImg参数为NULL,图片会绘制到当前窗口
3.w,h参数为图片缩放后的宽度和高度,若w和h都为0,代表不缩放原大小绘制
4.transparentcolor参数为指定的透明颜色,如果图片本身就是透明的,使用默认颜色BLACK

示例

#pragma once
#include <easyx.h>  
#pragma comment(lib,"MSIMG32.LIB")

// 在当前设备上绘制带透明通道的png图片
void PutImagePng(IMAGE* dstImg, int x, int y, int w, int h, IMAGE* srcImg, UINT transparentcolor)
{
	HDC dstDC = GetImageHDC(dstImg);
	HDC srcDC = GetImageHDC(srcImg);
	int width = srcImg->getwidth();
	int height = srcImg->getheight();
	//使用windows GDI函数实现透明位图
	TransparentBlt(dstDC, x, y, (w == 0 && h == 0) ? width : w, (w == 0 && h == 0) ? height:h, srcDC, 0, 0, width, height, transparentcolor);
}

int main()
{
	initgraph(600, 600);
	IMAGE img;
	loadimage(&img, "01.png");
	PutImagePng(NULL, 0, 0,0,0, &img, BLACK);
	while (1);
	closegraph();
	return 0;
}
posted @ 2026-01-09 21:04  让泪化作相思雨  阅读(13)  评论(0)    收藏  举报