Solved

Transparent bitmaps

Posted on 1997-05-31
5
374 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
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now