Virtual Developer Workshop: Containerized Development with Docker

This article demonstrates one technique for displaying a semi-transparent bitmap over another one (see image below). Other articles on CodeGuru show you how to display a bitmap with a transparent color (for example, all red pixels are not painted).

Example and source code

void DrawSemiTransparentBitmap(CDC *pDstDC, int x, int y, int nWidth, int nHeight,
    CDC* pSrcDC, int xSrc, int ySrc)
    CDC dcCompatible;
    CBitmap *pBitmapOld;
    CBitmap bm;

    bm.CreateCompatibleBitmap(pDstDC, nWidth, nHeight);
    pBitmapOld = dcCompatible.SelectObject(&bm);
    dcCompatible.FillSolidRect(CRect(0, 0, nWidth, nHeight), RGB(0x7F, 0x7F, 0x7F));
    pDstDC->BitBlt(x, y, nWidth, nHeight, &dcCompatible, 0, 0, SRCAND);

    pDstDC->BitBlt(x, y, nWidth, nHeight, pSrcDC, 0, 0, SRCPAINT);
The parameters of this functions are the same as BitBlt except the last one which is not used here.

More information

To display a bitmap (Say A) over another (B) to produce a result where A and B is visible you must divide each pixel of A by a constant, divide each pixel of B by another constant and add the two results.

Result = A*a + B*(1-a)    where "a" is a weight factor 
This method is much slower but produces better results than semi-transparent bitmaps. But if you are interested in speed you should use the semi-transparent method explained here.

The first BitBlt remove the higher bit of each pixel in the destination context and the second add the source context over the destination context. This will result in some saturation of bright colors but is not a problem in most applications.

Result = (A and 0x7F) + B
If you don't worry at all about saturation you can remove all but the last BitBlt. This will result in most case to an image with some saturation if you use bright colors.

Result = A + B
If A and B are big the result could be greater than 255 and result in saturation (modulo).


  • sample! sample! sample!!!

    Posted by mero_max on 11/14/2006 07:39am

    i wanna see sample too please attach sample please!

  • U should have given a sample project

    Posted by jayender.vs on 06/29/2006 10:23am

    Well dude, Its really looks good to see the semi transparent project , but it would have be even better (great) if u provide a sample project here, so that we can see how it is really done instead of thinking were to place the code .., if possible please attach a sample here. Ciao, Jay

  • Write your own damn sample program

    Posted by Legacy on 11/07/2003 08:00am

    Originally posted by: gatkinso

    Isn;t it enought that the author wrote the routine for you? Must you be spoon fed like an infant?

    • Sucks

      Posted by jayender.vs on 06/29/2006 10:27am

      Hello dude, Its worse to read a comment .. this is somthing that really helps many people here ,and its not difficult for the author to provide the sample project instead of giving the code...hope u understand. nothing wrong in providing the sample, it actually helps the people here who wants it very urgently instead of spending time in it .

    • Spoons

      Posted by dogbear on 03/31/2006 01:40pm


  • sample program

    Posted by Legacy on 10/08/2003 07:00am

    Originally posted by: karim

    Please is it possible to have this sample program
    Thanks a lot.
    best regards

  • Great! I looked for that formula for quiet a long time.

    Posted by Legacy on 11/29/2001 08:00am

    Originally posted by: Amit Gefen


  • I want a sample program, too

    Posted by Legacy on 09/04/2001 07:00am

    Originally posted by: choi chang-ho

    please! send me a sample program...

  • Now in Win98 and Win2K .. AlphaBlend()

    Posted by Legacy on 08/02/2001 07:00am

    Originally posted by: DarkByte

    Starting with Windows 98 .. there is a new Display Context function available called AlphaBlend() .. i haven't figured out how it works exactly but i'm working on it. Meanwhile, you guys can do your own research on it and probably come up with good examples.

    Here is the Syntax as show in the MSDN Library.

    BOOL AlphaBlend(
    HDC hdcDest, // handle to destination DC
    int nXOriginDest, // x-coord of upper-left corner
    int nYOriginDest, // y-coord of upper-left corner
    int nWidthDest, // destination width
    int nHeightDest, // destination height
    HDC hdcSrc, // handle to source DC
    int nXOriginSrc, // x-coord of upper-left corner
    int nYOriginSrc, // y-coord of upper-left corner
    int nWidthSrc, // source width
    int nHeightSrc, // source height
    BLENDFUNCTION blendFunction // alpha-blending function

    Have fun with this !

  • plz send me a sample program!!!

    Posted by Legacy on 07/16/2001 07:00am

    Originally posted by: fepest

    thank for you.
    i need sample program.
    plz. give me.

  • please give me source code

    Posted by Legacy on 09/04/2000 07:00am

    Originally posted by: yusun

    thank for you. 
    i need sample program.
    plz. give me.

  • Semitransparent Bitmap

    Posted by Legacy on 05/07/2000 07:00am

    Originally posted by: Rodney Herrmann

    Has anyone ever done anything similar to this with a window or dialog? I am interested in creating a window that is semi-transparent.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date