求大虾帮忙,使用C++对图片进行镂空+透明处理
void Sprite::DrawTransAlpha(HDC hdcBack,int nXBack,int nYBack,int nWidthBack,int nHeightBack,int nXFore,int nYFore,int nAlpha,COLORREF crTrans)
{
if(!m_bVisible)
return;
if(m_hbm!=NULL)
{
HDC hdcMem=CreateCompatibleDC(hdcBack);
RECT rBackRect={nXBack,nYBack,nWidthBack,nHeightBack};
HBITMAP hbmTmp=CreateCompatibleBitmap(hdcBack,nWidthBack,nHeightBack);
HBITMAP hbmBk=(HBITMAP)SelectObject(hdcBack,hbmTmp);
RECT rForeRect={nXFore,nYFore,nXFore+m_nWidth,nYFore+m_nHeight};
RECT rTransRect,rTransRect2;
if(IntersectRect(&rTransRect,&rBackRect,&rForeRect))
{
BITMAP bm,bmBk;
GetObject(m_hbm,sizeof(BITMAP),&bm);
BYTE *px=new unsigned char[bm.bmHeight*bm.bmWidthBytes];
GetBitmapBits(m_hbm,bm.bmHeight*bm.bmWidthBytes,px);
GetObject(hbmBk,sizeof(BITMAP),&bmBk);
BYTE *pxBk=new unsigned char[bmBk.bmHeight*bmBk.bmWidthBytes];
GetBitmapBits(hbmBk,bmBk.bmHeight*bmBk.bmWidthBytes,pxBk);
rTransRect2=rTransRect;
OffsetRect(&rTransRect2,-rForeRect.left,-rForeRect.top);
int pb=bm.bmBitsPixel/8;
int pbBk=bmBk.bmBitsPixel/8;
int dx1,dx2,dy1,dy2;//1back2fore
for(dy1=rTransRect.top,dy2=rTransRect2.top;dy1<rTransRect.bottom;dy1++,dy2++)
for(dx1=rTransRect.left,dx2=rTransRect2.left;dx1<rTransRect.right;dx1++,dx2++)
{
int nBackRGB_B=dy1*bmBk.bmWidthBytes+dx1*pbBk;
int nForeRGB_B=dy2*bm.bmWidthBytes+dx2*pb;
if(RGB(px[nForeRGB_B+2],px[nForeRGB_B+1],px[nForeRGB_B])!=crTrans)
{
pxBk[nBackRGB_B]=(pxBk[nForeRGB_B]*nAlpha+pxBk[nBackRGB_B]*(255-nAlpha))/255;
pxBk[nBackRGB_B+1]=(pxBk[nForeRGB_B+1]*nAlpha+pxBk[nBackRGB_B+1]*(255-nAlpha))/255;
pxBk[nBackRGB_B+2]=(pxBk[nForeRGB_B+2]*nAlpha+pxBk[nBackRGB_B+2]*(255-nAlpha))/255;
}
}
SetBitmapBits(hbmTmp,bmBk.bmHeight*bmBk.bmWidthBytes,pxBk);
HBITMAP hbmOld=(HBITMAP)SelectObject(hdcMem,hbmTmp);
BitBlt(hdcBack,0,0,rBackRect.right,rBackRect.bottom,hdcMem,0,0,SRCCOPY);
delete[] px;
delete[] pxBk;
SelectObject(hdcMem,hbmOld);
}
DeleteObject(hbmTmp);
DeleteDC(hdcMem);
}
}
上面的这段代码是我自己使用的,但效果很不理想,不知道大虾们是怎么处理的?
求改动,或者给小弟一段更高效的代码,使用镂空色的
我的意思是镂空+透明度同时处理的,如果是大段代码请发送到evil0angel@126.com万分感谢
{
if(!m_bVisible)
return;
if(m_hbm!=NULL)
{
HDC hdcMem=CreateCompatibleDC(hdcBack);
RECT rBackRect={nXBack,nYBack,nWidthBack,nHeightBack};
HBITMAP hbmTmp=CreateCompatibleBitmap(hdcBack,nWidthBack,nHeightBack);
HBITMAP hbmBk=(HBITMAP)SelectObject(hdcBack,hbmTmp);
RECT rForeRect={nXFore,nYFore,nXFore+m_nWidth,nYFore+m_nHeight};
RECT rTransRect,rTransRect2;
if(IntersectRect(&rTransRect,&rBackRect,&rForeRect))
{
BITMAP bm,bmBk;
GetObject(m_hbm,sizeof(BITMAP),&bm);
BYTE *px=new unsigned char[bm.bmHeight*bm.bmWidthBytes];
GetBitmapBits(m_hbm,bm.bmHeight*bm.bmWidthBytes,px);
GetObject(hbmBk,sizeof(BITMAP),&bmBk);
BYTE *pxBk=new unsigned char[bmBk.bmHeight*bmBk.bmWidthBytes];
GetBitmapBits(hbmBk,bmBk.bmHeight*bmBk.bmWidthBytes,pxBk);
rTransRect2=rTransRect;
OffsetRect(&rTransRect2,-rForeRect.left,-rForeRect.top);
int pb=bm.bmBitsPixel/8;
int pbBk=bmBk.bmBitsPixel/8;
int dx1,dx2,dy1,dy2;//1back2fore
for(dy1=rTransRect.top,dy2=rTransRect2.top;dy1<rTransRect.bottom;dy1++,dy2++)
for(dx1=rTransRect.left,dx2=rTransRect2.left;dx1<rTransRect.right;dx1++,dx2++)
{
int nBackRGB_B=dy1*bmBk.bmWidthBytes+dx1*pbBk;
int nForeRGB_B=dy2*bm.bmWidthBytes+dx2*pb;
if(RGB(px[nForeRGB_B+2],px[nForeRGB_B+1],px[nForeRGB_B])!=crTrans)
{
pxBk[nBackRGB_B]=(pxBk[nForeRGB_B]*nAlpha+pxBk[nBackRGB_B]*(255-nAlpha))/255;
pxBk[nBackRGB_B+1]=(pxBk[nForeRGB_B+1]*nAlpha+pxBk[nBackRGB_B+1]*(255-nAlpha))/255;
pxBk[nBackRGB_B+2]=(pxBk[nForeRGB_B+2]*nAlpha+pxBk[nBackRGB_B+2]*(255-nAlpha))/255;
}
}
SetBitmapBits(hbmTmp,bmBk.bmHeight*bmBk.bmWidthBytes,pxBk);
HBITMAP hbmOld=(HBITMAP)SelectObject(hdcMem,hbmTmp);
BitBlt(hdcBack,0,0,rBackRect.right,rBackRect.bottom,hdcMem,0,0,SRCCOPY);
delete[] px;
delete[] pxBk;
SelectObject(hdcMem,hbmOld);
}
DeleteObject(hbmTmp);
DeleteDC(hdcMem);
}
}
上面的这段代码是我自己使用的,但效果很不理想,不知道大虾们是怎么处理的?
求改动,或者给小弟一段更高效的代码,使用镂空色的
我的意思是镂空+透明度同时处理的,如果是大段代码请发送到evil0angel@126.com万分感谢
作者: evil0angel 发布时间: 2011-06-16
已发送。
作者: dizuo 发布时间: 2011-06-16
没看懂……
作者: evil0angel 发布时间: 2011-06-16