Transparent Bitmaps
-
I'm developing an application that allows the user to create a bitmap from portions of "n" other source bitmaps. For example, starting with three bitmaps (A, B, and C), the user could select the top third of A, the middle third of B and the bottom third of C and create destination bitmap D. What I'd like to do is create a window that shows one of bitmap A, B, C.... (and has a dialog bar allowing selection of which one). A, B, and C are fully formed, that is, all pixels are defined. Bitmap D is undefined, that is, all pixels are (by default) white. I'd like to display D as a translucent bitmap on top of the selected source bitmap. It the user left-click+moves the mouse over the pair of bitmaps the pixels on A are copied up into D. Once all desired pixels of A are copied upto D, the user would change A to B, D would now be displayed on top of B and the user could copied pixels from B upto D. And so on for all source bitmaps. The undefined parts of D (ie the white pixels) I'd like to show as a "gray film", and the cursor would then be an "eraser" ie remove the "film" and allows the underlying bitmap to shine through. In theory this is all fairly easy. BUT, I'm stymied on the simple part of how do I create and display the translucent bitmap D? Attempts so far are: 1. create aDC_ with aBitmap_ 2. create dDC_ with dBitmap_ 3. create memDC_ with memBitmap_ 4. memDC_->BitBlt(..., aDC_..., SRCCOPY) 5. memDC_->BitBlt(..., dDC_, ..., ?) <=== this is the translucent bitmap 6. pDC->BitBlt(..., memDC_..., SRCCOPY); The question is how do a setup the DC_'s to get the translucency to work? Any help is much appreciated! Regards, Kylur.
-
I'm developing an application that allows the user to create a bitmap from portions of "n" other source bitmaps. For example, starting with three bitmaps (A, B, and C), the user could select the top third of A, the middle third of B and the bottom third of C and create destination bitmap D. What I'd like to do is create a window that shows one of bitmap A, B, C.... (and has a dialog bar allowing selection of which one). A, B, and C are fully formed, that is, all pixels are defined. Bitmap D is undefined, that is, all pixels are (by default) white. I'd like to display D as a translucent bitmap on top of the selected source bitmap. It the user left-click+moves the mouse over the pair of bitmaps the pixels on A are copied up into D. Once all desired pixels of A are copied upto D, the user would change A to B, D would now be displayed on top of B and the user could copied pixels from B upto D. And so on for all source bitmaps. The undefined parts of D (ie the white pixels) I'd like to show as a "gray film", and the cursor would then be an "eraser" ie remove the "film" and allows the underlying bitmap to shine through. In theory this is all fairly easy. BUT, I'm stymied on the simple part of how do I create and display the translucent bitmap D? Attempts so far are: 1. create aDC_ with aBitmap_ 2. create dDC_ with dBitmap_ 3. create memDC_ with memBitmap_ 4. memDC_->BitBlt(..., aDC_..., SRCCOPY) 5. memDC_->BitBlt(..., dDC_, ..., ?) <=== this is the translucent bitmap 6. pDC->BitBlt(..., memDC_..., SRCCOPY); The question is how do a setup the DC_'s to get the translucency to work? Any help is much appreciated! Regards, Kylur.
here's the classic MSDN version:
BOOL DrawTransparentBitmap(HDC hdc,
HBITMAP hBitmap,
__int32 xStart,
__int32 yStart,
COLORREF cTransparentColor)
{BITMAP bm; COLORREF cColor; HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave; // lots of storage HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld, bmTempOld; HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave; // lots of dc's POINT ptSize; int iCaps = ::GetDeviceCaps(hdc, RASTERCAPS); if ((iCaps & RC\_BITBLT) != RC\_BITBLT) { return FALSE; } if ((iCaps & RC\_BITMAP64) != RC\_BITMAP64) { return FALSE; } hdcTemp = CreateCompatibleDC(hdc); if (hdcTemp==NULL) { return FALSE; } bmTempOld = (HBITMAP)SelectObject(hdcTemp, hBitmap); // Select the bitmap if (GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm) != sizeof(BITMAP))
{
DeleteDC(hdcTemp);
return FALSE;
}ptSize.x = bm.bmWidth; // Get width of bitmap ptSize.y = bm.bmHeight; // Get height of bitmap DPtoLP(hdcTemp, &ptSize, 1); // Convert from device // to logical points // Create some DCs to hold temporary data. hdcBack = CreateCompatibleDC(hdc); hdcObject = CreateCompatibleDC(hdc); hdcMem = CreateCompatibleDC(hdc); hdcSave = CreateCompatibleDC(hdc); // Create a bitmap for each DC. DCs are required for a number of // GDI functions. // Monochrome DC bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL); // Monochrome DC bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL); bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y); bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y); // Each DC must select a bitmap object to store pixel data. // (HBITMAP) Casts added by CDL, 3/26/96 bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack); bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject); bmMemOld = (HBITMAP)SelectObject(hdcMem, bmAndMem); bmSaveOld = (HBITMAP)SelectObject(hdcSave, bmSave); // Set proper mapping mode. SetMapMode(hdcTemp, GetMapMode(hdc)); // Save the bitmap sent here, because it will be overwritten. BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY); // Set the background color of the source DC to the color. // contained in the parts of the bitmap that should be transparent cColor = SetBkColor(hdcTemp, cTransparentColor); // Create the object mask for the bitmap by perfo
-
here's the classic MSDN version:
BOOL DrawTransparentBitmap(HDC hdc,
HBITMAP hBitmap,
__int32 xStart,
__int32 yStart,
COLORREF cTransparentColor)
{BITMAP bm; COLORREF cColor; HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave; // lots of storage HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld, bmTempOld; HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave; // lots of dc's POINT ptSize; int iCaps = ::GetDeviceCaps(hdc, RASTERCAPS); if ((iCaps & RC\_BITBLT) != RC\_BITBLT) { return FALSE; } if ((iCaps & RC\_BITMAP64) != RC\_BITMAP64) { return FALSE; } hdcTemp = CreateCompatibleDC(hdc); if (hdcTemp==NULL) { return FALSE; } bmTempOld = (HBITMAP)SelectObject(hdcTemp, hBitmap); // Select the bitmap if (GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm) != sizeof(BITMAP))
{
DeleteDC(hdcTemp);
return FALSE;
}ptSize.x = bm.bmWidth; // Get width of bitmap ptSize.y = bm.bmHeight; // Get height of bitmap DPtoLP(hdcTemp, &ptSize, 1); // Convert from device // to logical points // Create some DCs to hold temporary data. hdcBack = CreateCompatibleDC(hdc); hdcObject = CreateCompatibleDC(hdc); hdcMem = CreateCompatibleDC(hdc); hdcSave = CreateCompatibleDC(hdc); // Create a bitmap for each DC. DCs are required for a number of // GDI functions. // Monochrome DC bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL); // Monochrome DC bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL); bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y); bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y); // Each DC must select a bitmap object to store pixel data. // (HBITMAP) Casts added by CDL, 3/26/96 bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack); bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject); bmMemOld = (HBITMAP)SelectObject(hdcMem, bmAndMem); bmSaveOld = (HBITMAP)SelectObject(hdcSave, bmSave); // Set proper mapping mode. SetMapMode(hdcTemp, GetMapMode(hdc)); // Save the bitmap sent here, because it will be overwritten. BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY); // Set the background color of the source DC to the color. // contained in the parts of the bitmap that should be transparent cColor = SetBkColor(hdcTemp, cTransparentColor); // Create the object mask for the bitmap by perfo
-
GDI is not obsolete.