Quick Clear of Windows 32bit Bitmap to RGBA

Clearing a 32bit Bitmap on Windows to a specific BGRA in a fairly quick way.

Get information about the Bitmap so that we can gain access to the image bits:

    HBITMAP hbmpOld = (HBITMAP)SelectObject(CompatibleHDC, Bitmap);
    DIBSECTION info = { 0 };
    ::GetObject(Bitmap, sizeof(DIBSECTION), &info);

Clear the Bitmap to a known BGRA:

    uint32_t value = (((uint8_t)255) << 24) |
                          (Parameters.BackgroundBlue << 16) | 
                          (Parameters.BackgroundGreen << 8) | 
    unsigned char *bits = Bits;
    for (uint32_t row = 0; row < (uint32_t)info.dsBm.bmHeight; ++row)
       uint32_t *to_set = (uint32_t *)(((uint8_t*)info.dsBm.bmBits) +
                               (row * info.dsBm.bmWidthBytes));
       uint32_t *ptrEnd = (uint32_t*)((uint8_t *)to_set +

       while (to_set != ptrEnd)
         *to_set = value;

Alpha is being set to 255. All data is 4 byte aligned by definition on Windows in this case. Width of the bitmap is padded to 4 bytes and we've created a 32bit Bitmap.

Windows GDI only deals with BGR, unless you are using GDI+. GDI+ is however still not the answer for drawing with alpha on Windows, neither is Direct2D. A GDI draw will always set the alpha channel to 0.

The real difficult bit is picking an RGB value that does not appear in the output image you are drawing especially if you can't control the drawing.

You are going to have to post process the alpha channel after drawing on a per pixel basis.