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);
}
}
}