黑色透明png使用其他颜色填充(PPC或windows)

黑色透明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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值