Note that there are some explanatory texts on larger screens.

plurals
  1. POC++/Win32: How to get the alpha channel from an HBITMAP?
    primarykey
    data
    text
    <p>I have an <code>HBITMAP</code> containing alpha channel data. I can successfully render this using the <code>::AlphaBlend</code> GDI function. </p> <p>However, when I call the <code>::GetPixel</code> GDI function, I never get back values with an alpha component. The documentation does say that it returns the RGB value of the pixel.</p> <p>Is there a way to retrieve the alpha channel values for pixels in an <code>HBITMAP</code>?</p> <p>I want to be able to detect when to use ::AlphaBlend, and when to use an old-school method for treating a particular colour in the source HBITMAP as transparent.</p> <hr/> <pre><code>HDC sourceHdc = ::CreateCompatibleDC(hdcDraw); ::SelectObject(sourceHdc, m_hbmp); // This pixel has partial transparency, but ::GetPixel returns just RGB. COLORREF c = ::GetPixel(sourceHdc, 20, 20); // Draw the bitmap to hdcDraw BLENDFUNCTION bf1; bf1.BlendOp = AC_SRC_OVER; bf1.BlendFlags = 0; bf1.SourceConstantAlpha = 0xff; bf1.AlphaFormat = AC_SRC_ALPHA; ::AlphaBlend(di.hdcDraw, x, 10, 64, 64, sourceHdc, 0, 0, 64, 64, bf1); ::DeleteDC(sourceHdc); </code></pre> <hr/> <h2>Answer</h2> <p>Use GetDIBits to retrieve the first (or more) scan line(s) of the image:</p> <pre><code> byte* bits[1000];// = new byte[w * 4]; BITMAPINFO bmi; memset(&amp;bmi, 0, sizeof(BITMAPINFO)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = w; bmi.bmiHeader.biHeight = -h; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biSizeImage = 0; bmi.bmiHeader.biXPelsPerMeter = 0; bmi.bmiHeader.biYPelsPerMeter = 0; bmi.bmiHeader.biClrUsed = 0; bmi.bmiHeader.biClrImportant = 0; int rv = ::GetDIBits(sourceHdc1, m_hbmp, 0, 1, (void**)&amp;bits, &amp;bmi, DIB_RGB_COLORS); //bits[3] == alpha of topleft pixel; //delete[] bits; </code></pre>
    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.
 

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