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. GetBItmapBits()

GetBItmapBits()

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 2 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.
  • N Offline
    N Offline
    Neha
    wrote on last edited by
    #1

    Hi, I am loading a bmp file through LoadImage() and accessing the image data through GetBitmapBits(). Now i am saving the data as follows: BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi; CFile file; if( !file.Open( the_filename, CFile::modeWrite|CFile::modeCreate) ) return FALSE; lpbi = (LPBITMAPINFOHEADER)m_pFirstBuffer ; lpbi->biBitCount=24; int nColors = 1 << 24; if(nColors> 256) nColors=0; lpbi->biClrUsed=0; lpbi->biSize=40; lpbi->biHeight =(DWORD)bmp.bmHeight; lpbi->biWidth=(DWORD)bmp.bmWidth ; lpbi->biXPelsPerMeter =lpbi->biYPelsPerMeter =0; lpbi->biClrImportant =0; lpbi->biPlanes =1; lpbi->biCompression =0; lpbi->biSizeImage = bmp.bmHeight *bmp.bmWidthBytes ; // Fill in the fields of the file header hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM" hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + lpbi->biSize + lpbi->biClrUsed* sizeof(RGBQUAD) + lpbi->biSizeImage); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; if (lpbi->biBitCount > 0 && lpbi->biBitCount <= 8) { hdr.bfOffBits = (DWORD)(sizeof(hdr) + lpbi->biSize +nColors * sizeof(RGBQUAD)); } else { unsigned long cmapsize = 0; switch (lpbi->biBitCount) { case 16: case 32: if (lpbi->biCompression == BI_BITFIELDS) { cmapsize = 3*sizeof(DWORD); } break; case 24: cmapsize = lpbi->biClrUsed*sizeof(RGBQUAD); break; default: // no colormap break; } hdr.bfOffBits = (DWORD)(sizeof(hdr) + lpbi->biSize + cmapsize); } // Write the file header file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits file.Write( lpbi, lpbi->biSize ); file.Write( m_pFirstBuffer, bmp.bmHeight *bmp.bmWidthBytes ); file.Close (); Now i could open the saved file in Windows Paint application but if i call LoadImage() with that file name as parameter it returns NULL. GetLastError() is zero. Could anyone tell me where i am wrong? Regards Neha

    A 1 Reply Last reply
    0
    • N Neha

      Hi, I am loading a bmp file through LoadImage() and accessing the image data through GetBitmapBits(). Now i am saving the data as follows: BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi; CFile file; if( !file.Open( the_filename, CFile::modeWrite|CFile::modeCreate) ) return FALSE; lpbi = (LPBITMAPINFOHEADER)m_pFirstBuffer ; lpbi->biBitCount=24; int nColors = 1 << 24; if(nColors> 256) nColors=0; lpbi->biClrUsed=0; lpbi->biSize=40; lpbi->biHeight =(DWORD)bmp.bmHeight; lpbi->biWidth=(DWORD)bmp.bmWidth ; lpbi->biXPelsPerMeter =lpbi->biYPelsPerMeter =0; lpbi->biClrImportant =0; lpbi->biPlanes =1; lpbi->biCompression =0; lpbi->biSizeImage = bmp.bmHeight *bmp.bmWidthBytes ; // Fill in the fields of the file header hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM" hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + lpbi->biSize + lpbi->biClrUsed* sizeof(RGBQUAD) + lpbi->biSizeImage); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; if (lpbi->biBitCount > 0 && lpbi->biBitCount <= 8) { hdr.bfOffBits = (DWORD)(sizeof(hdr) + lpbi->biSize +nColors * sizeof(RGBQUAD)); } else { unsigned long cmapsize = 0; switch (lpbi->biBitCount) { case 16: case 32: if (lpbi->biCompression == BI_BITFIELDS) { cmapsize = 3*sizeof(DWORD); } break; case 24: cmapsize = lpbi->biClrUsed*sizeof(RGBQUAD); break; default: // no colormap break; } hdr.bfOffBits = (DWORD)(sizeof(hdr) + lpbi->biSize + cmapsize); } // Write the file header file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits file.Write( lpbi, lpbi->biSize ); file.Write( m_pFirstBuffer, bmp.bmHeight *bmp.bmWidthBytes ); file.Close (); Now i could open the saved file in Windows Paint application but if i call LoadImage() with that file name as parameter it returns NULL. GetLastError() is zero. Could anyone tell me where i am wrong? Regards Neha

      A Offline
      A Offline
      Abhishek Srivastava
      wrote on last edited by
      #2

      Hi Neha, Actually , While saving the BITMAP u converted it into Device independent Bitmap.and LoadImage API cannot load DIB's , it can load only icon, cursor, animated cursor, or bitmap or OEM Bitmaps. If u want to display this Bitmap, then u have to read the file by your own code ,get the BMP data in a buffer prepare headers, and u can use APIs like DrawDibDraw to draw ur image :) Regards Abhishek Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

      N 1 Reply Last reply
      0
      • A Abhishek Srivastava

        Hi Neha, Actually , While saving the BITMAP u converted it into Device independent Bitmap.and LoadImage API cannot load DIB's , it can load only icon, cursor, animated cursor, or bitmap or OEM Bitmaps. If u want to display this Bitmap, then u have to read the file by your own code ,get the BMP data in a buffer prepare headers, and u can use APIs like DrawDibDraw to draw ur image :) Regards Abhishek Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

        N Offline
        N Offline
        Neha
        wrote on last edited by
        #3

        I think you got confussed. I am using GetBitmapBits() it returns device dependent bitmap. In LoadImage() i am setting LOADFROMFILE. this will returns us the HBITMAP.I mean to say the value returned from LoadImage() ie HBITMAP is NULL.

        A 1 Reply Last reply
        0
        • N Neha

          I think you got confussed. I am using GetBitmapBits() it returns device dependent bitmap. In LoadImage() i am setting LOADFROMFILE. this will returns us the HBITMAP.I mean to say the value returned from LoadImage() ie HBITMAP is NULL.

          A Offline
          A Offline
          Abhishek Srivastava
          wrote on last edited by
          #4

          // Write the DIB header and the bits file.Write( lpbi, lpbi->biSize ); check these above line of ur code In ur Submitted code , u have created this DIB header, Which states that the BMP u created is DIB,and let me know if u the file extension is .bmp then that means u have created an DIB image , thats why LoadImage fails. Check few artilces for .bmp files , u can see these Windows supported Picture files are Device independent. Even if u used the GetBitmapBits , but while saving u converted it into DIB Regards Abhishek :) http://www.prepressure.com/formats/bmp/fileformat.htm Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

          N 1 Reply Last reply
          0
          • A Abhishek Srivastava

            // Write the DIB header and the bits file.Write( lpbi, lpbi->biSize ); check these above line of ur code In ur Submitted code , u have created this DIB header, Which states that the BMP u created is DIB,and let me know if u the file extension is .bmp then that means u have created an DIB image , thats why LoadImage fails. Check few artilces for .bmp files , u can see these Windows supported Picture files are Device independent. Even if u used the GetBitmapBits , but while saving u converted it into DIB Regards Abhishek :) http://www.prepressure.com/formats/bmp/fileformat.htm Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

            N Offline
            N Offline
            Neha
            wrote on last edited by
            #5

            My file extension is .DIB not .bmp. It works for some images but for some images it doesn't.

            A 1 Reply Last reply
            0
            • N Neha

              My file extension is .DIB not .bmp. It works for some images but for some images it doesn't.

              A Offline
              A Offline
              Abhishek Srivastava
              wrote on last edited by
              #6

              Well the extension doe'snt matter, what i think is that u r saving it as DIB :) and even ur extension says it is a DIB :-D.... try to read it as a DIB image may be it will solve ur issue!! Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

              N 1 Reply Last reply
              0
              • A Abhishek Srivastava

                Well the extension doe'snt matter, what i think is that u r saving it as DIB :) and even ur extension says it is a DIB :-D.... try to read it as a DIB image may be it will solve ur issue!! Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

                N Offline
                N Offline
                Neha
                wrote on last edited by
                #7

                Yes, I am wrong. Could you please tell me how to save the DDB data? I need to use the DDB format because DIB takes DWORD.

                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