SemiTransparent Bitmaps

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;

    dcCompatible.CreateCompatibleDC(pDstDC);
    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);
    dcCompatible.SelectObject(pBitmapOld);

    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).


Comments

  • sample! sample! sample!!!

    Posted by mero_max on 11/13/2006 11:39pm

    i wanna see sample too please attach sample please!

    Reply
  • U should have given a sample project

    Posted by jayender.vs on 06/29/2006 03: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

    Reply
  • Write your own damn sample program

    Posted by Legacy on 11/07/2003 12: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 03: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 .

      Reply
    • Spoons

      Posted by dogbear on 03/31/2006 05:40am

      Waaah!

      Reply
    Reply
  • sample program

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

    Originally posted by: karim

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

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

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

    Originally posted by: Amit Gefen

    Thnx

    Reply
  • I want a sample program, too

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

    Originally posted by: choi chang-ho

    please! send me a sample program...

    Reply
  • Now in Win98 and Win2K .. AlphaBlend()

    Posted by Legacy on 08/02/2001 12: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 !

    Reply
  • plz send me a sample program!!!

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

    Originally posted by: fepest

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

    Reply
  • please give me source code

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

    Originally posted by: yusun

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

    Reply
  • Semitransparent Bitmap

    Posted by Legacy on 05/07/2000 12: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.

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

  • Hurricane Sandy was one of the most destructive natural disasters that the United States has ever experienced. Read this success story to learn how Datto protected its partners and their customers with proactive business continuity planning, heroic employee efforts, and the right mix of technology and support. With storm surges over 12 feet, winds that exceeded 90 mph, and a diameter spanning more than 900 miles, Sandy resulted in power outages to approximately 7.5 million people, and caused an estimated $50 …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds