Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. CreateDIBitmap with 32bits RGBA data

CreateDIBitmap with 32bits RGBA data

Scheduled Pinned Locked Moved Graphics
graphicshelpquestion
12 Posts 3 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vlasta_
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • V Vlasta_

      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.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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:

      V 1 Reply Last reply
      0
      • M Mark Salsbery

        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:

        V Offline
        V Offline
        Vlasta_
        wrote on last edited by
        #3

        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...

        M 1 Reply Last reply
        0
        • V Vlasta_

          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...

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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:

          M V 2 Replies Last reply
          0
          • M Mark Salsbery

            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:

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • M Mark Salsbery

              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:

              V Offline
              V Offline
              Vlasta_
              wrote on last edited by
              #6

              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.

              V M 2 Replies Last reply
              0
              • V Vlasta_

                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.

                V Offline
                V Offline
                Vlasta_
                wrote on last edited by
                #7

                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.

                M 1 Reply Last reply
                0
                • V Vlasta_

                  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.

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  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:

                  1 Reply Last reply
                  0
                  • V Vlasta_

                    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.

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    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:

                    V 1 Reply Last reply
                    0
                    • M Mark Salsbery

                      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:

                      V Offline
                      V Offline
                      Vlasta_
                      wrote on last edited by
                      #10

                      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.

                      M 1 Reply Last reply
                      0
                      • V Vlasta_

                        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.

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #11

                        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:

                        V 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          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:

                          V Offline
                          V Offline
                          Vlasta_
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups