Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to draw 32-bit alpha channel bitmaps?
    primarykey
    data
    text
    <p>I need to create a custom control to display bmp images with alpha channel. The background can be painted in different colors and the images have shadows so I need to truly "paint" the alpha channel.</p> <p>Does anybody know how to do it?</p> <p>I also want if possible to create a mask using the alpha channel information to know whether the mouse has been click on the image or on the transparent area.</p> <p>Any kind of help will be appreciated!</p> <p>Thanks.</p> <p>Edited(JDePedro): As some of you have suggested I've been trying to use alpha blend to paint the bitmap with alpha channel. This just a test I've implemented where I load a 32-bit bitmap from resources and I try to paint it using AlphaBlend function:</p> <pre><code>void CAlphaDlg::OnPaint() { CClientDC dc(this); CDC dcMem; dcMem.CreateCompatibleDC(&amp;dc); CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP); BITMAP BitMap; bitmap.GetBitmap(&amp;BitMap); int nWidth = BitMap.bmWidth; int nHeight = BitMap.bmHeight; CBitmap *pOldBitmap = dcMem.SelectObject(&amp;bitmap); BLENDFUNCTION m_bf; m_bf.BlendOp = AC_SRC_OVER; m_bf.BlendFlags = 0; m_bf.SourceConstantAlpha = 255; m_bf.AlphaFormat = AC_SRC_ALPHA; AlphaBlend(dc.GetSafeHdc(), 100, 100, nWidth, nHeight, dcMem.GetSafeHdc(), 0, 0,nWidth, nHeight,m_bf); dcMem.SelectObject(pOldBitmap); CDialog::OnPaint(); } </code></pre> <p>This is just a test so I put the code in the OnPaint of the dialog (I also tried the AlphaBlend function of the CDC object).</p> <p>The non-transparent areas are being painted correctly but I get white where the bitmap should be transparent.</p> <p>Any help???</p> <p>This is a screenshot..it's not easy to see but there is a white rectangle around the blue circle: <a href="http://img385.imageshack.us/img385/7965/alphamh8.png">alt text http://img385.imageshack.us/img385/7965/alphamh8.png</a></p> <p>Ok. I got it! I have to pre-multiply every pixel for the alpha value. Someone can suggest the optimized way to do that?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload