VC++ 不规则窗体的实现(三)

GDI对Alpha通道图像贴图出现锯齿现象,对像素处理方法如下:

//对像素进行转换,否则会出现非透明的黑底或白底颜色
void CrossImage(CImage &img)
{
	if ( img.IsNull() )
		return ;

	//确认该图像包含Alpha通道
	if (img.GetBPP() != 32)
		return ;

	for(int i=0; i<img.GetWidth(); i++)
	{
		for(int j=0; j<img.GetHeight(); j++)
		{
			UCHAR *cr = (UCHAR*) img.GetPixelAddress(i,j);
			unsigned char alpha = cr[3];
			if ( alpha < 255 )
			{
				cr[0] = (cr[0]*cr[3] + 127) / 255;
				cr[1] = (cr[1]*cr[3] + 127) / 255;
				cr[2] = (cr[2]*cr[3] + 127) / 255;
			}
		}
	}
}

GDI+对Alpha通道图像贴图兼容性比较好,如果也需要对像素进行处理,可参考如下:

void CrossImage(Gdiplus::Bitmap* pImage)
{
    if (pImage->GetLastStatus() != Ok)
		goto exit;

	if (Gdiplus::IsAlphaPixelFormat((Gdiplus::PixelFormat)pImage->GetPixelFormat()))
	{
		Gdiplus::BitmapData source;
		Gdiplus::Rect rect(0, 0, pImage->GetWidth(), pImage->GetHeight());
		Gdiplus::Status status = pImage->LockBits(&rect, Gdiplus::ImageLockModeRead, pImage->GetPixelFormat(), &source);
		if (status == Gdiplus::Ok) 
		{
			for (int i = 0; i < pImage->GetWidth(); i++)
			{
				for (int j = 0; j < pImage->GetHeight(); j++)
				{
					UCHAR *cr = (UCHAR *)source.Scan0 + i * pImage->GetHeight() + j;// (UCHAR*)pImage->GetPixelAddress(i, j);
					unsigned char alpha = cr[3];
					if (alpha < 255)
					{						
						cr[0] = (cr[0] * cr[3] + 127) / 255;
						cr[1] = (cr[1] * cr[3] + 127) / 255;
						cr[2] = (cr[2] * cr[3] + 127) / 255;
						cr[3] = 100;
					}
				}
			}
			pImage->UnlockBits(&source);
		}
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值