:: TransparentBlt无法正常工作

时间:2013-12-01 09:48:01

标签: c++ winapi

我正在使用:: TransparentBlt绘制一个标有Magenta RGB(255,0,255)的透明像素的bmp,​​但是:: TransparentBlt表现不正常,一些不透明的像素在结果中是透明的

我做错了什么?

原始图片:

enter image description here

来自:: TransparentBlt的结果

enter image description here

结果图像中的灰色区域是背景图像,原始bmp是:: TransparentBlt-ed

代码:

LRESULT jItems::paint (  )
{
    HDC hdc ;
    PAINTSTRUCT ps ;
    RECT rect ; 
    ::GetClientRect ( hwnd , &rect ) ; 
    hdc = ::BeginPaint ( hwnd , &ps ) ;
    HDC dcSkin = ::CreateCompatibleDC ( hdc ); // memory dc for skin
    HDC hMemDc = ::CreateCompatibleDC ( hdc ); // memory dc for painting
    HBITMAP hmemBmp = ::CreateCompatibleBitmap ( hdc, rect.right - rect.left, rect.bottom - rect.top  ); // Create bitmap to draw on
    HBITMAP hOldMemBmp = ( HBITMAP ) ::SelectObject ( hMemDc, hmemBmp ); // select memory bitmap in memory dc
    HBITMAP hOldSkinBmp = ( HBITMAP ) ::SelectObject ( dcSkin, this->hbitmap ); //select skin bitmap in skin memory dc
    ::FillRect (  hMemDc, &rect, ::CreateSolidBrush ( backgroundColor )  );
    ::BitBlt ( hMemDc, 0, 0, rect.right - rect.left, 
        rect.bottom - rect.top, dcSkin, 0, 0, SRCCOPY ); // Paint Skin on Memory DC            
    ::SelectObject ( dcSkin, bottomEdge ); // select edge bitmap in skin memory dc
    ::TransparentBlt (  hMemDc, 0, 0, rect.right - rect.left, 
        rect.bottom - rect.top, dcSkin, 
        0, 0, 237, 10, RGB ( 255, 0, 255 )  ); // Paint edge on Memory DC 
    ::BitBlt ( hdc, 0, 0, rect.right - rect.left, 
        rect.bottom - rect.top, hMemDc, 0, 0, SRCCOPY ); // Paint Skin on Window DC  
     //<<<... DeleteDC will leak memory if it holds a resource, so lets select the old bitmap back in the memory DCs
    ::SelectObject ( hMemDc, hOldMemBmp );   // select old bitmaps back to their respective DCs before deleting
    ::SelectObject ( dcSkin, hOldSkinBmp );  // select old bitmaps back to their respective DCs before deleting
    ::DeleteObject ( hOldSkinBmp );
    ::DeleteObject ( hOldMemBmp );
    ::DeleteObject(  hmemBmp );
    ::DeleteDC ( hMemDc );
    ::DeleteDC ( dcSkin );
    ::EndPaint ( hwnd , &ps ) ;
    return 0;
};

1 个答案:

答案 0 :(得分:5)

来自TransparentBlt documentation

  

如果源矩形和目标矩形的大小不同,则会拉伸源位图以匹配目标矩形。

在对TransparentBlt的调用中,目标矩形的大小(rect.right - rect.left,rect.bottom - rect.top)和源矩形的大小(237,10)。因此,位图被拉伸,您无法得到预期的结果。

我猜两种尺寸都应该是(237,10)。