transp. blit over background image
-
I have a need to blit a .bmp over a backgound image, but not obscure areas under the foreground image where pixels are deemed to be transparent. I don't want the user to have to supply a mask image. I've read the CP articles on creating masks on the fly, but I just can't seem to wrap my mind around how to blend the two images. If someone can give me some guidance/pointers, I will be greatly appreciative. To make things even more fun, I'm coding in pure Win 32 - no MFC or ATL! Thanks in advance. things could always be worse...
-
I have a need to blit a .bmp over a backgound image, but not obscure areas under the foreground image where pixels are deemed to be transparent. I don't want the user to have to supply a mask image. I've read the CP articles on creating masks on the fly, but I just can't seem to wrap my mind around how to blend the two images. If someone can give me some guidance/pointers, I will be greatly appreciative. To make things even more fun, I'm coding in pure Win 32 - no MFC or ATL! Thanks in advance. things could always be worse...
This is the most known method unless you want to use
TransparentBlt
function. There is not much to say, other than the bitmap 'hBitmap' must not be already selected in another dc (only one dc can select a bitmap at a time!)void DrawTransparent(HDC hDC, int x, int y, int nWidth, int nHeight, HBITMAP hBitmap, COLORREF crColour)
{
_ASSERTE(hDC != NULL);
_ASSERTE(hBitmap != NULL);COLORREF crOldBack = SetBkColor(hDC, RGB(255,255,255)); COLORREF crOldText = SetTextColor(hDC, RGB(0,0,0)); HDC dcImage, dcMask, dcMem; // Create two memory dcs for the image and the mask dcImage = CreateCompatibleDC(hDC); dcMask = CreateCompatibleDC(hDC); dcMem = CreateCompatibleDC(hDC); HBITMAP hOldMemBitmap = (HBITMAP) SelectObject(dcMem, hBitmap); // Create bitmap copy HBITMAP hBitmapImage = CreateCompatibleBitmap(hDC, nWidth, nHeight); // Select the image into the appropriate dc HBITMAP hOldBitmapImage = (HBITMAP) SelectObject(dcImage, hBitmapImage); HBITMAP bitmapMask = CreateBitmap(nWidth, nHeight, 1, 1, NULL); // Select the mask bitmap into the appropriate dc HBITMAP hOldBitmapMask = (HBITMAP) SelectObject(dcMask, bitmapMask); // Build mask based on transparent colour BitBlt(dcImage, 0, 0, nWidth, nHeight, dcMem, 0, 0, SRCCOPY); SetBkColor(dcImage, crColour); BitBlt(dcMask, 0, 0, nWidth, nHeight, dcImage, 0, 0, SRCCOPY); // Do the work - True Mask method - cool if not actual display BitBlt(hDC, x, y, nWidth, nHeight, dcImage, 0, 0, SRCINVERT); BitBlt(hDC, x, y, nWidth, nHeight, dcMask, 0, 0, SRCAND); BitBlt(hDC, x, y, nWidth, nHeight, dcImage, 0, 0, SRCINVERT); // Restore settings SelectObject(dcImage, hOldBitmapImage); SelectObject(dcMask, hOldBitmapMask); SelectObject(dcMem, hOldMemBitmap); DeleteObject(hBitmapImage); DeleteObject(bitmapMask); DeleteDC( dcMem ); DeleteDC( dcMask ); DeleteDC( dcImage ); SetBkColor(hDC, crOldBack); SetTextColor(hDC, crOldText);
}
ÿVOTD: 8 "Love never fails. But where there are prophecies, they will cease; where there are tongues, they will be stilled; where there is knowledge, it will pass away." - 1 Cor 13:8 (NIV)
-
I have a need to blit a .bmp over a backgound image, but not obscure areas under the foreground image where pixels are deemed to be transparent. I don't want the user to have to supply a mask image. I've read the CP articles on creating masks on the fly, but I just can't seem to wrap my mind around how to blend the two images. If someone can give me some guidance/pointers, I will be greatly appreciative. To make things even more fun, I'm coding in pure Win 32 - no MFC or ATL! Thanks in advance. things could always be worse...
I figured it out. I case anyone else is interested, I did it as follows: // sets up the mask BitBlt(hdcDst,31,234,43,36,hdcSrc,31,234,SRCCOPY); clrOldText = SetTextColor(hdcSrc,RGB(255,255,255)); SetBkColor(hdcSrc,RGB(0,0,0)); BitBlt(hdcSrc,31,234,43,36,hdcTmp,31,234,SRCAND); BitBlt(hDC,31,234,43,36,hdcTmp,31,234,SRCAND); BitBlt(hDC,31,234,43,36,hdcSrc,31,234,SRCPAINT); If anyone sees any inherent flaws herein, please point them out. My colors looked right and nothing was obscured on the background, so I called it 'fixed'.:) things could always be worse...