Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use BitBlt to draw a transparent bitmap

804 views
Skip to first unread message

Eduardo

unread,
May 5, 2003, 9:07:36 AM5/5/03
to
I would like tor place a bitmap using pDC->BitBlt(...)
i already do it using SRCCOPY, now I need to transparent
it.
is there any raster operation to easy handle this, I know
that some drawing programs use Bottom-Left pixel as
transparent color, can I do something like that using
this method?

thanks in advance for your help,
Eduardo

Tim Magnusssen

unread,
May 5, 2003, 9:25:03 AM5/5/03
to
On XP and W2000 you can use TransparentBlt. On earlier versions of Windows
several operations are needed. Eg.

COLORREF cColor;
CBitmap bmAndBack, bmAndObject, bmAndMem, bmWorkCopy;
CBitmap *pOldbmMem, *pOldbmBack, *pOldbmObject, *pOldbmWorkCopy;
CDC hdcMem, hdcBack, hdcObject, hdcWorkCopy;

// Create some DCs to hold temporary data.
hdcBack.CreateCompatibleDC(pdc);
hdcObject.CreateCompatibleDC(pdc);
hdcMem.CreateCompatibleDC(pdc);
hdcWorkCopy.CreateCompatibleDC(pdc);

// Create a bitmap for each DC. DCs are required for a number of GDI
functions.

// Monochrome DC
bmAndBack.CreateBitmap(rcBounds.Width(), rcBounds.Height(), 1, 1, NULL);

// Monochrome DC
bmAndObject.CreateBitmap(rcBounds.Width(), rcBounds.Height(), 1, 1,
NULL);

bmAndMem.CreateCompatibleBitmap(pdc, rcBounds.Width(),
rcBounds.Height());
bmWorkCopy.CreateCompatibleBitmap(&hDC, rcBounds.Width(),
rcBounds.Height());

// Each DC must select a bitmap object to store pixel data.
pOldbmBack = hdcBack.SelectObject(&bmAndBack);
pOldbmObject = hdcObject.SelectObject(&bmAndObject);
pOldbmMem = hdcMem.SelectObject(&bmAndMem);

// Transfer bitmap to working object
pOldbmWorkCopy = hdcWorkCopy.SelectObject(&bmWorkCopy);
hdcWorkCopy.BitBlt(0, 0, rcBounds.Width(), rcBounds.Height(), &hDC, 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 = hdcWorkCopy.SetBkColor(cTransparentColor);

// Create the object mask for the bitmap by performing a BitBlt
// from the source bitmap to a monochrome bitmap.
hdcObject.BitBlt(0, 0, rcBounds.Width(), rcBounds.Height(),
&hdcWorkCopy, 0, 0, SRCCOPY);

// Set the background color of the source DC back to the original color.
hdcWorkCopy.SetBkColor(hDC.GetBkColor());

// Create the inverse of the object mask.
hdcBack.BitBlt(0, 0, rcBounds.Width(), rcBounds.Height(), &hdcObject, 0,
0, NOTSRCCOPY);

// Copy the background of the main DC to the destination.
hdcMem.BitBlt(0, 0, rcBounds.Width(), rcBounds.Height(), pdc,
rcBounds.left, rcBounds.top, SRCCOPY);

// Mask out the places where the bitmap will be placed.
hdcMem.BitBlt(0, 0, rcBounds.Width(), rcBounds.Height(), &hdcObject, 0,
0, SRCAND);

// Mask out the transparent colored pixels on the bitmap.
hdcWorkCopy.BitBlt(0, 0, rcBounds.Width(), rcBounds.Height(), &hdcBack,
0, 0, SRCAND);

// XOR the bitmap with the background on the destination DC.
hdcMem.BitBlt(0, 0, rcBounds.Width(), rcBounds.Height(), &hdcWorkCopy,
0, 0, SRCPAINT);

// Copy the destination to the screen.
pdc->BitBlt(rcBounds.left, rcBounds.top, rcBounds.Width(),
rcBounds.Height(), &hdcMem, 0, 0, SRCCOPY);

hdcBack.SelectObject(pOldbmBack);
hdcObject.SelectObject(pOldbmObject);
hdcMem.SelectObject(pOldbmMem);
hdcWorkCopy.SelectObject(pOldbmWorkCopy);


"Eduardo" <a...@a.com> wrote in message
news:04c701c31307$4c3a3dd0$a101...@phx.gbl...

Tim Magnusssen

unread,
May 5, 2003, 9:43:16 AM5/5/03
to
Btw. pdc is the target CDC, hDC is a reference to a memory DC containing the
transparent bitmap, rcBounds is a CRect containing the coordinates of the
bitmap and cTransparentColor is the transparent color.

void DrawTransparentDC(CDC *pdc, CDC &hDC, const CRect& rcBounds, COLORREF
cTransparentColor)

"Tim Magnusssen" <t...@get2net.dk> wrote in message
news:uDVN9mwE...@TK2MSFTNGP11.phx.gbl...

0 new messages