CreateDIBitmap with 32bits RGBA data
-
Hello all, I seem to have a problem creating bitmaps with alpha with the following code: BITMAPINFO tBMI; ZeroMemory (&tBMI, sizeof tBMI); tBMI.bmiHeader.biSize = sizeof tBMI.bmiHeader; tBMI.bmiHeader.biWidth = nSizeX; tBMI.bmiHeader.biHeight = -nSizeY; tBMI.bmiHeader.biPlanes = 1; tBMI.bmiHeader.biBitCount = 32; tBMI.bmiHeader.biCompression = BI_RGB; HBITMAP hBmp = CreateDIBitmap(hdc, &tBMI.bmiHeader, CBM_INIT, pData, &tBMI, DIB_RGB_COLORS); All works fine under normal circumstances, but if it happens that the data are completely transparent (all pixels have alpha set to 0), the behavior suddenly changes and instead of a completely transparent bitmap, the function chooses to ignore the alpha channel and creates a bitmap filled with black color (presumably because the RGB channels were set to 0 in the original data). Is this some Microsoft hack to maintain compatibility or what? Is there a way to prevent it? Thanks for any hints.
-
Hello all, I seem to have a problem creating bitmaps with alpha with the following code: BITMAPINFO tBMI; ZeroMemory (&tBMI, sizeof tBMI); tBMI.bmiHeader.biSize = sizeof tBMI.bmiHeader; tBMI.bmiHeader.biWidth = nSizeX; tBMI.bmiHeader.biHeight = -nSizeY; tBMI.bmiHeader.biPlanes = 1; tBMI.bmiHeader.biBitCount = 32; tBMI.bmiHeader.biCompression = BI_RGB; HBITMAP hBmp = CreateDIBitmap(hdc, &tBMI.bmiHeader, CBM_INIT, pData, &tBMI, DIB_RGB_COLORS); All works fine under normal circumstances, but if it happens that the data are completely transparent (all pixels have alpha set to 0), the behavior suddenly changes and instead of a completely transparent bitmap, the function chooses to ignore the alpha channel and creates a bitmap filled with black color (presumably because the RGB channels were set to 0 in the original data). Is this some Microsoft hack to maintain compatibility or what? Is there a way to prevent it? Thanks for any hints.
I know people argue with me here about this, but there really is very little support for ARGB in the old GDI bitmaps. Some functions may honor the alpha channel but many ignore it. The exception is DIBSections... Try CreateDIBSection instead - I think you'll find much more consistent results with your 32bpp ARGB bitmaps :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I know people argue with me here about this, but there really is very little support for ARGB in the old GDI bitmaps. Some functions may honor the alpha channel but many ignore it. The exception is DIBSections... Try CreateDIBSection instead - I think you'll find much more consistent results with your 32bpp ARGB bitmaps :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks for the answer. I have tried it, but unfortunately, the same "heuristics" applies to CreateDIBSection as well. I ended up using CreateIconFromResourceEx & DrawIconEx combination. Sounds a bit strange for such a "simple" task as displaying a RGBA image data...
-
Thanks for the answer. I have tried it, but unfortunately, the same "heuristics" applies to CreateDIBSection as well. I ended up using CreateIconFromResourceEx & DrawIconEx combination. Sounds a bit strange for such a "simple" task as displaying a RGBA image data...
Vlasta_ wrote:
but unfortunately, the same "heuristics" applies to CreateDIBSection as well.
Hmm, I guess that depends on how you display the bitmap. There's not many choices for that in GDI with ARGB data either.
Vlasta_ wrote:
I ended up using CreateIconFromResourceEx & DrawIconEx combination.
Ok, whatever works :) I personally prefer to use GDI+ for ARGB over something like that. Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Vlasta_ wrote:
but unfortunately, the same "heuristics" applies to CreateDIBSection as well.
Hmm, I guess that depends on how you display the bitmap. There's not many choices for that in GDI with ARGB data either.
Vlasta_ wrote:
I ended up using CreateIconFromResourceEx & DrawIconEx combination.
Ok, whatever works :) I personally prefer to use GDI+ for ARGB over something like that. Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Agreed, I'm using GDI+ for 32bpp bitmaps in a project now and it's worked just fine.
--Mike-- Visual C++ MVP :cool: LINKS~! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen
-
Vlasta_ wrote:
but unfortunately, the same "heuristics" applies to CreateDIBSection as well.
Hmm, I guess that depends on how you display the bitmap. There's not many choices for that in GDI with ARGB data either.
Vlasta_ wrote:
I ended up using CreateIconFromResourceEx & DrawIconEx combination.
Ok, whatever works :) I personally prefer to use GDI+ for ARGB over something like that. Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Hmmm... I'd like to have the applications compatible with many Windows versions (including Windows 98 and NT4). It is OK, if things look ugly on the older versions, but the application should run. So, I was drawing using an imagelist with this single bitmap. Maybe the optimization was inside the image list.
-
Hmmm... I'd like to have the applications compatible with many Windows versions (including Windows 98 and NT4). It is OK, if things look ugly on the older versions, but the application should run. So, I was drawing using an imagelist with this single bitmap. Maybe the optimization was inside the image list.
gdiplus could do it, but...it would increase the size of the installer too much for just this simple task. and who knows, maybe it will have the same problem ;-) (it is built upon the classic gdi, isn't it?) besides I like agg better for the vector stuff ...but thanks for the recommendation, it is a good advice, my needs are just a bit special.
-
gdiplus could do it, but...it would increase the size of the installer too much for just this simple task. and who knows, maybe it will have the same problem ;-) (it is built upon the classic gdi, isn't it?) besides I like agg better for the vector stuff ...but thanks for the recommendation, it is a good advice, my needs are just a bit special.
Vlasta_ wrote:
it is built upon the classic gdi, isn't it?
No. It is an extension. It does a lot of stuff GDI does not. :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hmmm... I'd like to have the applications compatible with many Windows versions (including Windows 98 and NT4). It is OK, if things look ugly on the older versions, but the application should run. So, I was drawing using an imagelist with this single bitmap. Maybe the optimization was inside the image list.
As long as you found a solution, there's nothing wrong with that! :) As for the imagelist, maybe a ILC_COLOR32 imagelist would work, since that uses a dibsection internally, but I don't see any support to render an image from the imagelist using the alpha channel values. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
As long as you found a solution, there's nothing wrong with that! :) As for the imagelist, maybe a ILC_COLOR32 imagelist would work, since that uses a dibsection internally, but I don't see any support to render an image from the imagelist using the alpha channel values. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
yes, I was using ILC_COLOR32 and it worked correctly in all cases except when every pixels' alpha was 0. So the weird behavior only appeared in this single case, it was enough to change the alpha of the first pixel to 1 and it suddenly started to work as expected. That's why I was calling it "optimization" ;-). I believe it is worth mentioning somewhere in the documentation... something like: BTW, dear Windows user if you create completely transparent 32-bits bitmap, we will ignore the alpha channel.
-
yes, I was using ILC_COLOR32 and it worked correctly in all cases except when every pixels' alpha was 0. So the weird behavior only appeared in this single case, it was enough to change the alpha of the first pixel to 1 and it suddenly started to work as expected. That's why I was calling it "optimization" ;-). I believe it is worth mentioning somewhere in the documentation... something like: BTW, dear Windows user if you create completely transparent 32-bits bitmap, we will ignore the alpha channel.
Vlasta_ wrote:
That's why I was calling it "optimization"
Not a very optimal optimization. :) What function were you using to draw the bitmap? I'd like to play with that a bit. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Vlasta_ wrote:
That's why I was calling it "optimization"
Not a very optimal optimization. :) What function were you using to draw the bitmap? I'd like to play with that a bit. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
With ImageList_Draw. Hm, "optimization" is not a good word. Older Windows did not support alpha at all and when the support was introduced, the functions were probably extended to handle it. But, I assume, due to compatibility issues someone decided to incorporate this "optimization" and when 32bit bitmap is used and it is completely transparent, it kicks in.