title: Adding Alpha channel to bitmap or converting bitmap to 32bit
body: <p><strong>SOLVED:</strong>
  Current code sample:</p>
  
  <pre><code>HBITMAP TwoBitmap(HDC &amp;hdc, RECT &amp;rc)
  {
      HDC hdcmem = CreateCompatibleDC(hdc);
      HBITMAP hbitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left, rc.bottom-rc.top);
      SelectObject(hdcmem, hbitmap);
  
      HBRUSH hBrush = GetSysColorBrush(2);
      FillRect(hdcmem, &amp;rc, hBrush);
      DeleteObject((HBRUSH)hBrush);
  
      DeleteDC(hdcmem);
  
      return hbitmap;
  }
  
  void CreateTransparentBitmap(HBITMAP &amp;hBitmap)
  {
      BITMAP TemporaryBitmap;
      GetObject(hBitmap, sizeof(BITMAP), &amp;TemporaryBitmap);
  
      HDC MemoryDC = CreateCompatibleDC(NULL);
  
      BITMAPINFO BitmapInfo;
      ZeroMemory(&amp;BitmapInfo,sizeof(BITMAPINFO));
      BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
      BitmapInfo.bmiHeader.biWidth = TemporaryBitmap.bmWidth;
      BitmapInfo.bmiHeader.biHeight = TemporaryBitmap.bmHeight;
      BitmapInfo.bmiHeader.biPlanes = 1;
      BitmapInfo.bmiHeader.biBitCount = 32;
      BitmapInfo.bmiHeader.biCompression = BI_RGB;
  
      RGBQUAD* pArgb = NULL;
      HBITMAP hTemporaryBitmap = CreateDIBSection(MemoryDC, &amp;BitmapInfo, DIB_RGB_COLORS, (void**)&amp;pArgb, NULL, 0);
  
      GetDIBits(MemoryDC, hBitmap, 0, BitmapInfo.bmiHeader.biHeight, pArgb, &amp;BitmapInfo, DIB_RGB_COLORS);
  
      for (int i = 0; i &lt; BitmapInfo.bmiHeader.biWidth * BitmapInfo.bmiHeader.biHeight; i++)
          pArgb[i].rgbReserved = 255;
  
      DeleteDC(MemoryDC);
  
      DeleteObject(hBitmap);
      hBitmap = hTemporaryBitmap;
  }
  
  case WM_PAINT:
      {
          PAINTSTRUCT p;
          HDC hDC = BeginPaint(hWnd, &amp;p);
          HDC hdcmem = CreateCompatibleDC(hDC);
  
          HBITMAP hImage = TwoBitmap(hDC, p.rcPaint);
  
          CreateTransparentBitmap(hImage);
  
          SelectObject(hdcmem, hImage);
  
          BLENDFUNCTION bfn;
          bfn.BlendOp = AC_SRC_OVER;
          bfn.BlendFlags = 0;
          bfn.SourceConstantAlpha = 10;
          bfn.AlphaFormat = AC_SRC_ALPHA;
  
          AlphaBlend(hDC, 0, 0, 100, 64, hdcmem, 0, 0, 100, 64, bfn);
  
          bfn.SourceConstantAlpha = 80;
          AlphaBlend(hDC, 200, 0, 100, 64, hdcmem, 0, 0, 100, 64, bfn);
  
          bfn.SourceConstantAlpha = 255;
          AlphaBlend(hDC, 400, 0, 100, 64, hdcmem, 0, 0, 100, 64, bfn);
  
          BitBlt(hDC, 0, 100, 100, 64, hdcmem, 0, 0, SRCCOPY);
  
          DeleteDC(hdcmem);
  
          DeleteObject(hImage);
  
          EndPaint(hWnd, &amp;p);
      }
      break;
  </code></pre>
  
  <hr>
  
  <p>For this example I have a simple bitmap created using CreateCompatibleBitmap():</p>
  
  <pre><code>HBITMAP TwoBitmap(HDC &amp;hdc, RECT &amp;rc)
  {
      HDC hdcmem = CreateCompatibleDC(hdc);
      HBITMAP hbitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left, 
                                               rc.bottom-rc.top);
      SelectObject(hdcmem, hbitmap);
  
      HBRUSH hBrush = GetSysColorBrush(3);
      FillRect(hdcmem, &amp;rc, hBrush);
      DeleteObject((HBRUSH)hBrush);
  
      DeleteDC(hdcmem);
  
      return hbitmap;
  }
  </code></pre>
  
  <p>Now, what I am trying to achieve is to add alpha channel to this bitmap (if it doesn't have it already). Using the code below I try to create a 32 bit bitmap and then copy old bitmap into this new one. And finally I print out new bitmap to screen using AlphaBlend() and BitBlt() functions and as usual I get monochromatic alpha blend images and perfectly fine BitBlt image. </p>
  
  <p>So there goes my question - what am I missing, doing wrong? I'm pretty lost here. My code (don't mind GDI leaks):</p>
  
  <p>EDIT:</p>
  
  <p>Thanks to <strong>PhoenixX_2</strong> I was able to solve the mystery, all I needed was to set Alpha bit to 255. Here is my code below, keep in mind that it isn't leak-free.</p>
  
  <pre><code>PAINTSTRUCT p;
  HDC hDC = BeginPaint(hWnd, &amp;p);
  
  HDC BufferDC = CreateCompatibleDC(NULL);
  
  BITMAPINFO BufferInfo;
  ZeroMemory(&amp;BufferInfo,sizeof(BITMAPINFO));
  BufferInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  BufferInfo.bmiHeader.biWidth = p.rcPaint.right-p.rcPaint.left;
  BufferInfo.bmiHeader.biHeight = p.rcPaint.bottom-p.rcPaint.top;
  BufferInfo.bmiHeader.biPlanes = 1;
  BufferInfo.bmiHeader.biBitCount = 32;
  BufferInfo.bmiHeader.biCompression = BI_RGB;
  RGBQUAD* pArgb;
  HBITMAP BufferBitmap = CreateDIBSection(BufferDC, &amp;BufferInfo, DIB_RGB_COLORS,
                                         (void**)&amp;pArgb, NULL, 0);
  
  HBITMAP hImage  =  TwoBitmap(hDC, p.rcPaint);
  
  GetDIBits(BufferDC, hImage, 0, BufferInfo.bmiHeader.biHeight, pArgb, 
            &amp;BufferInfo, DIB_RGB_COLORS);
  
  //----------------------------------the part I was missing
  for (int i = 0; i &lt; BufferInfo.bmiHeader.biWidth 
       * BufferInfo.bmiHeader.biHeight; i++)
  pArgb[i].rgbReserved = 255;
  //----------------------------------
  
  SelectObject(BufferDC, BufferBitmap);
  
  BLENDFUNCTION bfn;
  bfn.BlendOp = AC_SRC_OVER;
  bfn.BlendFlags = 0;
  bfn.SourceConstantAlpha = 10;
  bfn.AlphaFormat = AC_SRC_ALPHA;
  
  AlphaBlend(hDC, 0, 0, 100, 64, BufferDC, 0, 0, 100, 64, bfn);
  
  bfn.SourceConstantAlpha = 80;
  AlphaBlend(hDC, 200, 0, 100, 64, BufferDC, 0, 0, 100, 64, bfn);
  
  bfn.SourceConstantAlpha = 255;
  AlphaBlend(hDC, 400, 0, 100, 64, BufferDC, 0, 0, 100, 64, bfn);
  
  BitBlt(hDC, 0, 100, 100, 64, BufferDC, 0, 0, SRCCOPY);
  
  DeleteDC(BufferDC);
  
  DeleteObject((HBITMAP)BufferBitmap);
  
  EndPaint(hWnd, &amp;p);
  </code></pre>