winapimfcgdi+gdicmfctoolbar

How to set alpha value for all pixels in a bitmap using MFC or GDI or GDI+


I am in an MFC application. I created a bitmap using a memory DC I want to save it to DIB file.

I found this code to be most elegant so far:

void Save(CBitmap * bitmap) {
  CImage image;
  image.Attach((HBITMAP)pcBitmap->GetSafeHandle());
  image.Save("bla.bmp", Gdiplus::ImageFormatBMP);
}

The resulting file is 32 BPP colorspace with all alpha values set to '0'.

Now I want use the Bitmap as toolbar bitmap:

CMFCToolbar::GetImages()->Load("bla.bmp");

But all the icons are gone.

MFC internally calls PreMultiplyAlpha() when importing the bitmap. Then RGB components of all pixels are '0'. Effectively the whole bitmap was zeroed.

How can I set the alpha value for each pixel to '0xFF' before saving?

I tried:

void Save(CBitmap * bitmap) {
  CImage image;
  image.Attach((HBITMAP)pcBitmap->GetSafeHandle());
  image.SetHasAlphaChannel(true);
  image.AlphaBlend(myBitmapDC, 0, 0);
  image.Save("bla.bmp", Gdiplus::ImageFormatBMP);
}

But that affects only RGB values of the pixels.

So far I resisted to iterate over each pixel and modifying the memory of the bitmap. I'm asking for an elegant solution. Maybe a one-liner.


Solution

  • Use GetDIBits to read 32-bit pixel data, and loop through the bits to set alpha to 0xFF.

    bool Save(CBitmap *bitmap)
    {
        if(!bitmap)
            return false;
    
        BITMAP bm;
        bitmap->GetBitmap(&bm);
            if(bm.bmBitsPixel < 16)
                return false;
    
        DWORD size = bm.bmWidth * bm.bmHeight * 4;
        BITMAPINFOHEADER bih = { sizeof(bih), bm.bmWidth, bm.bmHeight, 1, 32, BI_RGB };
        BITMAPFILEHEADER bfh = { 'MB', 54 + size, 0, 0, 54 };
    
        CClientDC dc(0);
        std::vector<BYTE> vec(size, 0xFF);
        int test = GetDIBits(dc, *bitmap, 0, bm.bmHeight, &vec[0],
                (BITMAPINFO*)&bih, DIB_RGB_COLORS);
        for(DWORD i = 0; i < size; i += 4)
            vec[i + 3] = 0xFF;
    
        CFile fout;
        if(fout.Open(filename, CFile::modeCreate | CFile::modeWrite))
        {
            fout.Write(&bfh, sizeof(bfh));
            fout.Write(&bih, sizeof(bih));
            fout.Write(&vec[0], size);
            return true;
        }
    
        return false;
    }
    


    As an alternative (but I am not sure if this is reliable) initialize the memory with 0xFF. GetDIBits will set the RGB part but won't overwrite the alpha values:

    std::vector<BYTE> vec(size, 0xFF);
    GetDIBits...
    


    Or using GDI+

    bool Save(CBitmap *bitmap)
    {
        if(!bitmap)
            return false;
    
        BITMAP bm;
        bitmap->GetBitmap(&bm);
        if(bm.bmBitsPixel < 16)
            return false; //needs palette
    
        Gdiplus::GdiplusStartupInput tmp;
        ULONG_PTR token;
        Gdiplus::GdiplusStartup(&token, &tmp, NULL);
    
        Gdiplus::Bitmap *src = Gdiplus::Bitmap::FromHBITMAP(*bitmap, NULL);
        Gdiplus::Bitmap *dst = src->Clone(0, 0, src->GetWidth(), src->GetHeight(), 
            PixelFormat32bppARGB);
    
        LPCOLESTR clsid_bmp = L"{557cf400-1a04-11d3-9a73-0000f81ef32e}";
        CLSID clsid;
        CLSIDFromString(clsid_bmp, &clsid);
        bool result = dst->Save(L"file.bmp", &clsid) == 0;
        delete src;
        delete dst;
    
        Gdiplus::GdiplusShutdown(token);
    
        return result;
    }