Transparent bitmaps

This is the second time I have to ask this question.
I need to make sections of a bitmap transparent in Borland 5  using the OWL libraries. The first answer I recieved was in MFC. I tried converting the relevant functions but it still didn't work. I am trying to paint the bitmap in the paint function. The background is variable and itcould change each time the window is repainted, so simple masking is not appropriate. Thank you.
Who is Participating?
RONSLOWConnect With a Mentor Commented:
You can use image lists to do transparency - I haven't donw that myself, but may be able to russle up some sample code from elsewhere.

I have some straight C code which does transparency if that will help you.

Basically, transparentcy involves determining the transparant colour, creating a monochrome mask bitmap from the original, then doing the bitmaping using that mask.  Under NT there is a MaskBlt available o make this easier, otherwise it involves three separate BitBlt's

Bitmaps doesn't support transparent colors. How about this way: use AVI-files which support it. Make AVI which contain only one phase and use it instead bitmap. Hope it help.
Why won't masking work?  Each time you paint, get the bitmap of the rectangle you are about to paint over, and mask on top of that...
vrvshAuthor Commented:
Could you please show me the C code. What I don't understand is how I can make only one mask, if the background in variable. Does the mask make a color transparent or does it just replace a pixel with the pixel behind the bitmap.
The mask is a bitmap with only 0 and 1 at each pixel.  You use SRCAND as your BitBlt raster operator.

If the mask pixel is 0, then you don't copy the source bitmap onto the destination for that pixel (ie. the background is unchanged).  If the maks pixel is 1, then you DO copy the source onto the destination for that pixel.

Here are the steps involved:

1.Create an "AND mask"
 a. Set the background to transparent color
 b. Create a monochrome DC.
 c. BitBlt the image into the monochrome DC.
Pixels in transparent color map to '1' and others to '0'

2.Use BitBlt with SRCAND to copy the AND mask onto the destination DC (non transparent pixels cleared)

3.Use BitBlt with SRCAND to copy the inverse of the AND mask onto the image DC (transparent pixels cleared)

4.Use BitBlt with SRCPAINT to copy the image DC onto the destination DC (combines the two)

Here is C source code...

void DrawTransparentBitmap(
  HDC hdc,
  HBITMAP hBitmap,
  short xStart, short yStart,
  COLORREF cTransparentColor
) {
   BITMAP     bm;
   COLORREF   cColor;
   HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;
   HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
   HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
   POINT      ptSize;

   hdcTemp = CreateCompatibleDC(hdc);
   SelectObject(hdcTemp, hBitmap);   // Select the bitmap

   GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
   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.
   bmBackOld   = SelectObject(hdcBack, bmAndBack);
   bmObjectOld = SelectObject(hdcObject, bmAndObject);
   bmMemOld    = SelectObject(hdcMem, bmAndMem);
   bmSaveOld   = 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 performing a BitBlt
   // from the source bitmap to a monochrome bitmap.
   BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,

   // Set the background color of the source DC back to the original
   // color.
   SetBkColor(hdcTemp, cColor);

   // Create the inverse of the object mask.
   BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,

   // Copy the background of the main DC to the destination.
   BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,

   // Mask out the places where the bitmap will be placed.
   BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);

   // Mask out the transparent colored pixels on the bitmap.
   BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);

   // XOR the bitmap with the background on the destination DC.
   BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);

   // Copy the destination to the screen.
   BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,

   // Place the original bitmap back into the bitmap sent here.
   BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);

   // Delete the memory bitmaps.
   DeleteObject(SelectObject(hdcBack, bmBackOld));
   DeleteObject(SelectObject(hdcObject, bmObjectOld));
   DeleteObject(SelectObject(hdcMem, bmMemOld));
   DeleteObject(SelectObject(hdcSave, bmSaveOld));

   // Delete the memory DCs.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.