[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Transparent bitmaps

Posted on 1997-05-31
5
Medium Priority
?
396 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:vrvsh
5 Comments
 
LVL 4

Expert Comment

by:AVaulin
ID: 1163775
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.
0
 

Expert Comment

by:laleonard
ID: 1163776
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...
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 1163777
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

0
 

Author Comment

by:vrvsh
ID: 1163778
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.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163779
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,
          SRCCOPY);

   // 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,
          NOTSRCCOPY);

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

   // 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,
          SRCCOPY);

   // 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.
   DeleteDC(hdcMem);
   DeleteDC(hdcBack);
   DeleteDC(hdcObject);
   DeleteDC(hdcSave);
   DeleteDC(hdcTemp);
   }
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question