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. C / C++ / MFC
  4. DIBs and BMP's

DIBs and BMP's

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
10 Posts 3 Posters 0 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.
  • T Offline
    T Offline
    Tommy2k
    wrote on last edited by
    #1

    In my project, i just PNG's for the GUI's graphics. I can paint them on the canvas using StretchDIBits. Problem is that my project has some dialogs too, in which i use Static controls to hold a BMP image. Is there a way to convert a DIB image to a BMP handle that i can use in such a dialog other than writing the Dib as a BMP to the harddisk and loading it again?

    G R 2 Replies Last reply
    0
    • T Tommy2k

      In my project, i just PNG's for the GUI's graphics. I can paint them on the canvas using StretchDIBits. Problem is that my project has some dialogs too, in which i use Static controls to hold a BMP image. Is there a way to convert a DIB image to a BMP handle that i can use in such a dialog other than writing the Dib as a BMP to the harddisk and loading it again?

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      The following steps should do it:

      1. Create a bitmap compatible with the screen device context.
      2. Create a memory device context compatible with the screen.
      3. Select the bitmap created in step 1 into the DC created in step 2. Be sure to save the return value, so you can restore the original bitmap.
      4. Draw the PNG into the memory device context using StretchDIBits, or whatever's appropriate.
      5. Restore the original bitmap into the memory device context. This 'detaches' the bitmap created in step 1 from the memory device context.

      The bitmap created in step 1 now contains the image from the PNG, rendered in a fashion that's compatible with the screen. You can then use SetBitmap to display the image in the static controls in your dialog.


      Software Zen: delete this;

      T 1 Reply Last reply
      0
      • T Tommy2k

        In my project, i just PNG's for the GUI's graphics. I can paint them on the canvas using StretchDIBits. Problem is that my project has some dialogs too, in which i use Static controls to hold a BMP image. Is there a way to convert a DIB image to a BMP handle that i can use in such a dialog other than writing the Dib as a BMP to the harddisk and loading it again?

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        If you've got a HBITMAP handle for your DIB, then the static control will display it directly. To create an HBITMAP from DIB data, use CreateDIBitmap(). Hope this helps Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
        Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

        G T 2 Replies Last reply
        0
        • R Ryan Binns

          If you've got a HBITMAP handle for your DIB, then the static control will display it directly. To create an HBITMAP from DIB data, use CreateDIBitmap(). Hope this helps Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
          Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          Nice answer. I had a feeling mine was too complicated :-O.


          Software Zen: delete this;

          R 1 Reply Last reply
          0
          • G Gary R Wheeler

            Nice answer. I had a feeling mine was too complicated :-O.


            Software Zen: delete this;

            R Offline
            R Offline
            Ryan Binns
            wrote on last edited by
            #5

            :-O Thanks. I got a bit confused when he started out talking about PNGs, but then he said his question was how to convert a DIB to a HBITMAP, so that bit was easy :). I just assumed the PNG to be a typo or unnecessary information. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
            Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

            1 Reply Last reply
            0
            • G Gary R Wheeler

              The following steps should do it:

              1. Create a bitmap compatible with the screen device context.
              2. Create a memory device context compatible with the screen.
              3. Select the bitmap created in step 1 into the DC created in step 2. Be sure to save the return value, so you can restore the original bitmap.
              4. Draw the PNG into the memory device context using StretchDIBits, or whatever's appropriate.
              5. Restore the original bitmap into the memory device context. This 'detaches' the bitmap created in step 1 from the memory device context.

              The bitmap created in step 1 now contains the image from the PNG, rendered in a fashion that's compatible with the screen. You can then use SetBitmap to display the image in the static controls in your dialog.


              Software Zen: delete this;

              T Offline
              T Offline
              Tommy2k
              wrote on last edited by
              #6

              well i did it this way: HDC hDC = GetDC(); HDC memDC = CreateCompatibleDC ( hDC ); HBITMAP memBM = CreateCompatibleBitmap (hDC,dib->biWidth,dib->biHeight); HBITMAP memBM2 = CreateCompatibleBitmap (hDC,dib->biWidth,dib->biHeight); memBM2 = (HBITMAP)SelectObject ( memDC, memBM ); StretchDIBits(memDC,0,0,dib->biWidth,dib->biHeight,0,0,dib->biWidth,dib->biHeight,lpbits,(LPBITMAPINFO)&dib,DIB_RGB_COLORS,SRCCOPY); memBM = (HBITMAP)SelectObject(memDC,memBM2); ReleaseDC(memDC); CStatic temp = GetDlgItem (IDC_LOGO); temp.SetBitmap((HBITMAP)memBM); and it only draws a black square...size is ok though...

              R 1 Reply Last reply
              0
              • R Ryan Binns

                If you've got a HBITMAP handle for your DIB, then the static control will display it directly. To create an HBITMAP from DIB data, use CreateDIBitmap(). Hope this helps Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                T Offline
                T Offline
                Tommy2k
                wrote on last edited by
                #7

                I dont think i can use this sollution because i only have these things handy: BITMAPINFOHEADER dib; int dib_size, dib_bits_offs; char * lpbits; So i don't see how i can work with that functions...

                R 1 Reply Last reply
                0
                • T Tommy2k

                  I dont think i can use this sollution because i only have these things handy: BITMAPINFOHEADER dib; int dib_size, dib_bits_offs; char * lpbits; So i don't see how i can work with that functions...

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #8

                  That's all you need. The BITMAPINFO structure is simply a BITMAPINFOHEADER followed by a colour table, which I assume you've got. If your image is 16-bit or above, no colour table is needed, so you don't have to worry about it. For a 16, 24, or 32-bit image, just do this:

                  BITMAPINFO bi = { dib, 0 };
                  HBITMAP hbm = CreateDIBitmap(hDC, &dib, CBM_INIT, lpbits, &bi, DIB_RGB_COLORS);

                  For a 256 or less colour image, you'll need to include the colour table in the BITMAPINFO structure. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                  Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                  T 1 Reply Last reply
                  0
                  • T Tommy2k

                    well i did it this way: HDC hDC = GetDC(); HDC memDC = CreateCompatibleDC ( hDC ); HBITMAP memBM = CreateCompatibleBitmap (hDC,dib->biWidth,dib->biHeight); HBITMAP memBM2 = CreateCompatibleBitmap (hDC,dib->biWidth,dib->biHeight); memBM2 = (HBITMAP)SelectObject ( memDC, memBM ); StretchDIBits(memDC,0,0,dib->biWidth,dib->biHeight,0,0,dib->biWidth,dib->biHeight,lpbits,(LPBITMAPINFO)&dib,DIB_RGB_COLORS,SRCCOPY); memBM = (HBITMAP)SelectObject(memDC,memBM2); ReleaseDC(memDC); CStatic temp = GetDlgItem (IDC_LOGO); temp.SetBitmap((HBITMAP)memBM); and it only draws a black square...size is ok though...

                    R Offline
                    R Offline
                    Ryan Binns
                    wrote on last edited by
                    #9

                    You don't need to create the second bitmap, as it is returned by SelectObject(). Also, you're overwriting memBM with the second SelectObject(). Try this:

                    HDC hDC = GetDC();
                    HDC memDC = CreateCompatibleDC ( hDC );
                    HBITMAP memBM = CreateCompatibleBitmap (hDC,dib->biWidth,dib->biHeight);
                    HBITMAP memBM2;
                    memBM2 = (HBITMAP)SelectObject ( memDC, memBM );

                    StretchDIBits(memDC,0,0,dib->biWidth,dib->biHeight,0,0,dib->biWidth,dib->biHeight,lpbits,
                    (LPBITMAPINFO)&dib,DIB_RGB_COLORS,SRCCOPY);

                    SelectObject(memDC,memBM2);
                    ReleaseDC(memDC);

                    CStatic *temp = (CStatic*)GetDlgItem (IDC_LOGO);
                    temp->SetBitmap((HBITMAP)memBM);

                    Also make sure that your BITMAPINFOHEADER is setup correctly. I assume it is because you mentioned you can draw it elsewhere. If the image is 256 colours or less, the BITMAPINFOHEADER structure is NOT equivalent to a BITMAPINFO structure. The BITMAPINFO structure contains a BITMAPINFOHEADER plus the colour table. If the image has more than 256 colours, the colour table is not used and not required. For less than 256 colours it is. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                    Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                    1 Reply Last reply
                    0
                    • R Ryan Binns

                      That's all you need. The BITMAPINFO structure is simply a BITMAPINFOHEADER followed by a colour table, which I assume you've got. If your image is 16-bit or above, no colour table is needed, so you don't have to worry about it. For a 16, 24, or 32-bit image, just do this:

                      BITMAPINFO bi = { dib, 0 };
                      HBITMAP hbm = CreateDIBitmap(hDC, &dib, CBM_INIT, lpbits, &bi, DIB_RGB_COLORS);

                      For a 256 or less colour image, you'll need to include the colour table in the BITMAPINFO structure. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                      Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                      T Offline
                      T Offline
                      Tommy2k
                      wrote on last edited by
                      #10

                      Great :-D, it works! Thank you all :-)

                      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