黑色透明png使用其他颜色填充:
int SGDrawImageWithColor( HDC hdc, const RECT* pdstRect, SGImageInfo* pImageinfo, COLORREF crFillColor )
{IImage* pImage = pImageinfo->pImage;
if( pImage ) {
int hOffset = (*pdstRect).left;
int vOffset = (*pdstRect).top;
RECT rcArea = {0,0, (*pdstRect).right - (*pdstRect).left, (*pdstRect).bottom-(*pdstRect).top};
HDC hMemDC = CreateCompatibleDC(hdc);
if (hMemDC) {
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, rcArea.right-rcArea.left, rcArea.bottom-rcArea.top);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
//COLORREF crbkg = RGB(180,180,180);
//FillSolidRect( hMemDC, &rcArea, crbkg);
BitBlt( hMemDC, 0, 0, rcArea.right-rcArea.left, rcArea.bottom-rcArea.top, hdc, hOffset, vOffset, SRCCOPY );
pImage->Draw( hMemDC, &rcArea, NULL);
//calculate alpha
COLORREF color = 0;
COLORREF crbkg = 0;
for( int i = rcArea.left; i < rcArea.right ; ++i) {
for ( int j = rcArea.top; j < rcArea.bottom; ++j )
{
crbkg = GetPixel(hdc, i+hOffset, j+vOffset);
color = GetPixel(hMemDC, i, j);
float rc = (GetRValue(color)-0);
float rb = (GetRValue(crbkg)-0);
float alpha = 0.0;
if( rb <= 0 ) {
rc = (GetGValue(color)-0);
rb = (GetGValue(crbkg)-0);
if( rb <= 0 ) {
rc = (GetBValue(color)-0);
rb = (GetBValue(crbkg)-0);
}
}
if( rb > 0 ) {
alpha = rc/rb;
}
if( alpha < 1.0 ) {
//printf( " rgb(%d,%d) alpha = %f \n ", i,j, alpha );
//color = GetPixel(hdc, i+hOffset, j+vOffset);
int r = (1-alpha)*GetRValue(crFillColor)+alpha*GetRValue(crbkg);
int g = (1-alpha)*GetGValue(crFillColor)+alpha*GetGValue(crbkg);
int b = (1-alpha)*GetBValue(crFillColor)+alpha*GetBValue(crbkg);
//SetPixel( hdc, i+hOffset, j+vOffset, RGB(r,g,b) );
SetPixel( hMemDC, i, j, RGB(r,g,b) );
}
}
}
BitBlt( hdc, hOffset, vOffset, rcArea.right-rcArea.left, rcArea.bottom-rcArea.top, hMemDC, 0, 0, SRCCOPY );
SelectObject(hMemDC, hOldBitmap);
DeleteObject( hBitmap );
DeleteDC(hMemDC);
}
return 1;
}
return -1;
}