2
votes

I'm currently editing some old GDI code to use GDI+ and ran into a problem when it comes to draw a BMP file with transparent background. The old GDI code did not use any obvious extra code to draw the background transparent so I'm wondering how to achieve this using GDI+.

My current code looks like this

HINSTANCE hinstance = GetModuleHandle(NULL);
bmp = Gdiplus::Bitmap::FromResource(hinstance, MAKEINTRESOURCEW(IDB_BMP));
Gdiplus::Graphics graphics(pDC->m_hDC);
graphics.DrawImage(&bmp, posX, posY);

I also tried to create a new bitmap from the resource by using the clone method and by drawing the bitmap to a newly created one but neither did help. Both times I used PixelFormat32bppPARGB.

Then I tried to use alpha blending but this way the whole image gets transparent and not only the background:

Gdiplus::ColorMatrix clrMatrix = { 
    1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.5f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.0f, 1.0f
};

Gdiplus::ImageAttributes imgAttr;
imgAttr.SetColorMatrix(&clrMatrix);

graphics.DrawImage(&bmp, destRect, 0, 0, width(), height(), Gdiplus::UnitPixel, &imgAttr);

The transparency information is already contained in the image but I don't have a clue how to apply it when drawing the image. How does one achieve this?

3

3 Answers

1
votes

The simplest solution is to use some format other than BMP.

You need the bits to contain alpha data, and you need the Bitmap to be in a format that has alpha data. When you load a BMP with GDI+, it will always use a format without alpha, even if the BMP has an alpha channel. I believe the data is there in the image bits, but it's not being used.

The problem when you clone or draw to a PixelFormat32bppPARGB Bitmap is that GDI+ will convert the image data to the new format, which means discarding the alpha data.

Assuming it's loading the bits correctly, what you need to do is copy the bits over directly to another bitmap with the correct format. You can do this with Bitmap::LockBits and Bitmap::UnlockBits. (Make sure you lock each bitmap with its native pixel format so no conversion is done.)

2
votes

A late answer but:

ImageAttributes imAtt;    
imAtt.SetColorKey(Color(255,255,255), Color(255,255,255), ColorAdjustTypeBitmap);  

Will make white (255,255,255) transparent on any bitmap you use this image attribute with.

0
votes

I had the same problem. Transparent BMPs have not been shown correctly and unfortunately, PNGs cannot be loaded directly from resources (except by adding quite a bit of code which copies them into a stream and loads them from the stream). I wanted to avoid this code.
The bitmaps that I'm using also use only two colours (background and logo). Having an alpha channel means that I would need to save them with a much higher colour depth instead of only 2 bit colour depth.

Evan's answer was exactly was I was looking for :-)

Instead of white, I'm using the colour of the top left pixel as transparent colour:

Gdiplus::Color ColourOfTopLeftPixel;
Gdiplus::Status eStatus = m_pBitmap->GetPixel(0, 0, &ColourOfTopLeftPixel);
_ASSERTE(eStatus == Gdiplus::Ok);

// The following makes every pixel with the same colour as the top left pixel (ColourOfTopLeftPixel) transparent.
Gdiplus::ImageAttributes ImgAtt;
ImgAtt.SetColorKey(ColourOfTopLeftPixel, ColourOfTopLeftPixel, Gdiplus::ColorAdjustTypeBitmap);