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. Get image into DIB

Get image into DIB

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestionhelptutorial
5 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.
  • J Offline
    J Offline
    Jerry Wang 0
    wrote on last edited by
    #1

    Hi, Can anyone tell me how I can move an image into DIB? I hard coded my program to display images onto the screen correctly with 32bit/pixel, and now the program doesn't work when the screen is set to another color bit depth. Chris Becke's GDI tutorial was very helpful and led me to the GetDIBits and SetDIBits functions. I am planning to use GetDIBits(HDC, HBITMAP...) for converting my image to DIB, but I don't know what I should put in for HDC. The only context I can think of is my screenDC, but I doubt it is looking for my screenDC, rather, it's looking for the DC I originate my bitmap from... Is this correct? If so, then how can I create a DC for my image? My image is a string of BYTES with xBGRxBGRxBGRxBGR... where x is an unused byte. Thanks in advance for the help!:-O

    P 1 Reply Last reply
    0
    • J Jerry Wang 0

      Hi, Can anyone tell me how I can move an image into DIB? I hard coded my program to display images onto the screen correctly with 32bit/pixel, and now the program doesn't work when the screen is set to another color bit depth. Chris Becke's GDI tutorial was very helpful and led me to the GetDIBits and SetDIBits functions. I am planning to use GetDIBits(HDC, HBITMAP...) for converting my image to DIB, but I don't know what I should put in for HDC. The only context I can think of is my screenDC, but I doubt it is looking for my screenDC, rather, it's looking for the DC I originate my bitmap from... Is this correct? If so, then how can I create a DC for my image? My image is a string of BYTES with xBGRxBGRxBGRxBGR... where x is an unused byte. Thanks in advance for the help!:-O

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      try:

      HBITMAP hDib = (HBITMAP)::CopyImage(bmp, IMAGE\_BITMAP, 0, 0, LR\_COPYRETURNORG | LR\_CREATEDIBSECTION);
      

      'bmp' can be a DIB, DDB or CBitmap, the returned 'hDib' will be a DIB. --- It may be that your sole purpose in life is simply to serve as a warning to others.

      J 1 Reply Last reply
      0
      • P PJ Arends

        try:

        HBITMAP hDib = (HBITMAP)::CopyImage(bmp, IMAGE\_BITMAP, 0, 0, LR\_COPYRETURNORG | LR\_CREATEDIBSECTION);
        

        'bmp' can be a DIB, DDB or CBitmap, the returned 'hDib' will be a DIB. --- It may be that your sole purpose in life is simply to serve as a warning to others.

        J Offline
        J Offline
        Jerry Wang 0
        wrote on last edited by
        #3

        Great! Thanks for the CopyImage tip. I'll give it a try. One more question, how does CopyImage know the bit depth of 'bmp'? Is there a flag in there where I set? This is how I am creating my bitmap: CClientDC screenDC(pView); bitmapBefore.CreateCompatibleBitmap(&screenDC, sizeBefore.cx, sizeBefore.cy); bitmapBefore.SetBitmapBits(sizeBefore.cy*sizeBefore.cx*4, imageBuffer); where imageBuffer is my Byte string with xBGRxBGRxBGR... Right now image only displays properly for 32bit/pixel displaying modes, and that's because I'm grabing DC from screen. Since the screen and my image are both 32bit/pixel, the image displays correctly. How do I make the bitmap have the information of 32bit/pixel when my screen DC is not? I have to replace CreateCompatibilityBitmap with something else don't I? Thanks again!:)

        P 1 Reply Last reply
        0
        • J Jerry Wang 0

          Great! Thanks for the CopyImage tip. I'll give it a try. One more question, how does CopyImage know the bit depth of 'bmp'? Is there a flag in there where I set? This is how I am creating my bitmap: CClientDC screenDC(pView); bitmapBefore.CreateCompatibleBitmap(&screenDC, sizeBefore.cx, sizeBefore.cy); bitmapBefore.SetBitmapBits(sizeBefore.cy*sizeBefore.cx*4, imageBuffer); where imageBuffer is my Byte string with xBGRxBGRxBGR... Right now image only displays properly for 32bit/pixel displaying modes, and that's because I'm grabing DC from screen. Since the screen and my image are both 32bit/pixel, the image displays correctly. How do I make the bitmap have the information of 32bit/pixel when my screen DC is not? I have to replace CreateCompatibilityBitmap with something else don't I? Thanks again!:)

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #4

          Ok, if I understand you correctly, what you have is the dimensions of a bitmap (width and height), and an array of BYTEs specifying the data bits for a 32 bit bitmap, but you do not have the bitmap itself. You can create the bitmap using the CreateDIBitmap() or CreateDIBSection() functions. The HDC used just has to a HDC compatible with the screen.

          HDC DC = ::CreateCompatibleDC(NULL);

          BITMAPINFO bmi;
          ::ZeroMemory(&bmi, sizeof(BITMAPINFO));
          bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
          bmi.bmiHeader.biWidth = width;
          bmi.bmiHeader.biHeight = height;
          bmi.bmiHeader.biPlanes = 1;
          bmi.bmiHeader.biBitcount = 32;
          bmi.bmiHeader.biCompression = BI_RGB;
          bmi.bmiHeader.biSizeImage = width * height * 4;

          void *dummy;
          HBITMAP hDIB = ::CreateDIBSection(DC, &bmi, DIB_RGB_COLORS, &dummy, NULL, 0);
          ::SetDIBits(DC, hDIB, 0, height, Pointer_to_Data_Bits_Array, &bmi, DIB_RGB_COLORS);

          --- It may be that your sole purpose in life is simply to serve as a warning to others.

          L 1 Reply Last reply
          0
          • P PJ Arends

            Ok, if I understand you correctly, what you have is the dimensions of a bitmap (width and height), and an array of BYTEs specifying the data bits for a 32 bit bitmap, but you do not have the bitmap itself. You can create the bitmap using the CreateDIBitmap() or CreateDIBSection() functions. The HDC used just has to a HDC compatible with the screen.

            HDC DC = ::CreateCompatibleDC(NULL);

            BITMAPINFO bmi;
            ::ZeroMemory(&bmi, sizeof(BITMAPINFO));
            bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            bmi.bmiHeader.biWidth = width;
            bmi.bmiHeader.biHeight = height;
            bmi.bmiHeader.biPlanes = 1;
            bmi.bmiHeader.biBitcount = 32;
            bmi.bmiHeader.biCompression = BI_RGB;
            bmi.bmiHeader.biSizeImage = width * height * 4;

            void *dummy;
            HBITMAP hDIB = ::CreateDIBSection(DC, &bmi, DIB_RGB_COLORS, &dummy, NULL, 0);
            ::SetDIBits(DC, hDIB, 0, height, Pointer_to_Data_Bits_Array, &bmi, DIB_RGB_COLORS);

            --- It may be that your sole purpose in life is simply to serve as a warning to others.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Thank you so much PJ for the code. It worked like a charm. I'm able to display images in different bit depths now :)

            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