Reputation: 2323
I am using ::TransparentBlt to paint a bmp with transparent pixels marked by Magenta RGB(255, 0, 255), but ::TransparentBlt is not behaving properly, some pixels which shouldn't be transparent are transparent in the result.
What am I doing wrong ?
Original Image :
Result from ::TransparentBlt
The grey area in the result image is background image on top of which the original bmp is ::TransparentBlt-ed
Code:
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;
};
Upvotes: 0
Views: 1851
Reputation: 16896
From the TransparentBlt documentation.
If the source and destination rectangles are not the same size, the source bitmap is stretched to match the destination rectangle.
In your call to TransparentBlt the destination rectangle has size (rect.right - rect.left, rect.bottom - rect.top) and the source rectangle has size (237, 10). So the bitmap is stretched and you don't get the result you expected.
I guess both sizes should be (237, 10).
Upvotes: 5