C++ – Using AlphaBlend() and FillRect()

cgdi+winapi

So, I'm using AlphaBlend() to copy a rectangle from one HBITMAP to another.

It works, but there is a problem. Whenever I use the FillRect() function, the alpha values in the HBITMAP get slammed out to 0. Everytime.

So I have to GetDIBits(), reset the alpha back to 255, and then SetDIBits(), after every call to the Win32 api functions like FillRect().

So, is there a way to create an HBRUSH or otherwise tell FillRect() not to touch the alpha channel values in the HBITMAP it is going to be drawing to?

Best Answer

With the exception of AlphaBlend... BitBlt is the only other GDI function that will preserve the alpha channel in any way.

Your options basically therefore are:

  1. Switch to using DIBSections. This will not solve the basic problem of GDI apis overwiting the alpha channel, but as a DIBSection you can avoid the costly DDB -> DIB -> DDB transformation needed to patch up the alpha channel. DIBSections give you access to both an HBITMAP and a memory section where the bitmaps bits are stored.

  2. Use an alpha aware painting API like GdiPlus instead of GDI.