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. Savin a bitmap, but

Savin a bitmap, but

Scheduled Pinned Locked Moved C / C++ / MFC
comgraphics
4 Posts 4 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.
  • I Offline
    I Offline
    Ivan Cachicatari
    wrote on last edited by
    #1

    Hi, I want to save a bitmap in a file. My funcion is the following code, but this save the a inverted bitmap. BOOL CDXCaptureDlg::SaveBMP(BYTE *Buffer, int width, int height, long paddedsize, char *bmpfile) { // declare bmp structures BITMAPFILEHEADER bmfh; BITMAPINFOHEADER info; // andinitialize them to zero memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) ); memset ( &info, 0, sizeof (BITMAPINFOHEADER ) ); // fill the fileheader with data bmfh.bfType = 0x4d42; // 0x4d42 = 'BM' bmfh.bfReserved1 = 0; bmfh.bfReserved2 = 0; bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize; bmfh.bfOffBits = 0x36; // number of bytes to start of bitmap bits // fill the infoheader info.biSize = sizeof(BITMAPINFOHEADER); info.biWidth = width; info.biHeight = height; info.biPlanes = 1; // we only have one bitplane info.biBitCount = 24; // RGB mode is 24 bits info.biCompression = BI_RGB; info.biSizeImage = 0; // can be 0 for 24 bit images info.biXPelsPerMeter = 0x0ec4; // paint and PSP use this values info.biYPelsPerMeter = 0x0ec4; info.biClrUsed = 0; // we are in RGB mode and have no palette info.biClrImportant = 0; // all colors are important // now we open the file to write to HANDLE file = CreateFile ( bmpfile , GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( file == NULL ) { CloseHandle ( file ); return FALSE; } // write file header unsigned long bwritten; if ( WriteFile ( file, &bmfh, sizeof ( BITMAPFILEHEADER ), &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // write infoheader if ( WriteFile ( file, &info, sizeof ( BITMAPINFOHEADER ), &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // write image data if ( WriteFile ( file, Buffer, paddedsize, &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // and close file CloseHandle ( file ); return TRUE; } Ivan Cachicatari Blog[^] www.latindevelopers.com

    R B N 3 Replies Last reply
    0
    • I Ivan Cachicatari

      Hi, I want to save a bitmap in a file. My funcion is the following code, but this save the a inverted bitmap. BOOL CDXCaptureDlg::SaveBMP(BYTE *Buffer, int width, int height, long paddedsize, char *bmpfile) { // declare bmp structures BITMAPFILEHEADER bmfh; BITMAPINFOHEADER info; // andinitialize them to zero memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) ); memset ( &info, 0, sizeof (BITMAPINFOHEADER ) ); // fill the fileheader with data bmfh.bfType = 0x4d42; // 0x4d42 = 'BM' bmfh.bfReserved1 = 0; bmfh.bfReserved2 = 0; bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize; bmfh.bfOffBits = 0x36; // number of bytes to start of bitmap bits // fill the infoheader info.biSize = sizeof(BITMAPINFOHEADER); info.biWidth = width; info.biHeight = height; info.biPlanes = 1; // we only have one bitplane info.biBitCount = 24; // RGB mode is 24 bits info.biCompression = BI_RGB; info.biSizeImage = 0; // can be 0 for 24 bit images info.biXPelsPerMeter = 0x0ec4; // paint and PSP use this values info.biYPelsPerMeter = 0x0ec4; info.biClrUsed = 0; // we are in RGB mode and have no palette info.biClrImportant = 0; // all colors are important // now we open the file to write to HANDLE file = CreateFile ( bmpfile , GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( file == NULL ) { CloseHandle ( file ); return FALSE; } // write file header unsigned long bwritten; if ( WriteFile ( file, &bmfh, sizeof ( BITMAPFILEHEADER ), &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // write infoheader if ( WriteFile ( file, &info, sizeof ( BITMAPINFOHEADER ), &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // write image data if ( WriteFile ( file, Buffer, paddedsize, &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // and close file CloseHandle ( file ); return TRUE; } Ivan Cachicatari Blog[^] www.latindevelopers.com

      R Offline
      R Offline
      Russell
      wrote on last edited by
      #2

      Ivan Cachicatari wrote:

      inverted bitmap

      It means color inversion, image transposition (like matrix) or other ?

      Have a nice code day ;)

      1 Reply Last reply
      0
      • I Ivan Cachicatari

        Hi, I want to save a bitmap in a file. My funcion is the following code, but this save the a inverted bitmap. BOOL CDXCaptureDlg::SaveBMP(BYTE *Buffer, int width, int height, long paddedsize, char *bmpfile) { // declare bmp structures BITMAPFILEHEADER bmfh; BITMAPINFOHEADER info; // andinitialize them to zero memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) ); memset ( &info, 0, sizeof (BITMAPINFOHEADER ) ); // fill the fileheader with data bmfh.bfType = 0x4d42; // 0x4d42 = 'BM' bmfh.bfReserved1 = 0; bmfh.bfReserved2 = 0; bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize; bmfh.bfOffBits = 0x36; // number of bytes to start of bitmap bits // fill the infoheader info.biSize = sizeof(BITMAPINFOHEADER); info.biWidth = width; info.biHeight = height; info.biPlanes = 1; // we only have one bitplane info.biBitCount = 24; // RGB mode is 24 bits info.biCompression = BI_RGB; info.biSizeImage = 0; // can be 0 for 24 bit images info.biXPelsPerMeter = 0x0ec4; // paint and PSP use this values info.biYPelsPerMeter = 0x0ec4; info.biClrUsed = 0; // we are in RGB mode and have no palette info.biClrImportant = 0; // all colors are important // now we open the file to write to HANDLE file = CreateFile ( bmpfile , GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( file == NULL ) { CloseHandle ( file ); return FALSE; } // write file header unsigned long bwritten; if ( WriteFile ( file, &bmfh, sizeof ( BITMAPFILEHEADER ), &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // write infoheader if ( WriteFile ( file, &info, sizeof ( BITMAPINFOHEADER ), &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // write image data if ( WriteFile ( file, Buffer, paddedsize, &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // and close file CloseHandle ( file ); return TRUE; } Ivan Cachicatari Blog[^] www.latindevelopers.com

        B Offline
        B Offline
        benjymous
        wrote on last edited by
        #3

        Do you mean it's upside-down? That's the way the BMP format works, the first row in the data is the bottom line, and so on up to the top. -- Help me! I'm turning into a grapefruit! Buzzwords!

        1 Reply Last reply
        0
        • I Ivan Cachicatari

          Hi, I want to save a bitmap in a file. My funcion is the following code, but this save the a inverted bitmap. BOOL CDXCaptureDlg::SaveBMP(BYTE *Buffer, int width, int height, long paddedsize, char *bmpfile) { // declare bmp structures BITMAPFILEHEADER bmfh; BITMAPINFOHEADER info; // andinitialize them to zero memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) ); memset ( &info, 0, sizeof (BITMAPINFOHEADER ) ); // fill the fileheader with data bmfh.bfType = 0x4d42; // 0x4d42 = 'BM' bmfh.bfReserved1 = 0; bmfh.bfReserved2 = 0; bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize; bmfh.bfOffBits = 0x36; // number of bytes to start of bitmap bits // fill the infoheader info.biSize = sizeof(BITMAPINFOHEADER); info.biWidth = width; info.biHeight = height; info.biPlanes = 1; // we only have one bitplane info.biBitCount = 24; // RGB mode is 24 bits info.biCompression = BI_RGB; info.biSizeImage = 0; // can be 0 for 24 bit images info.biXPelsPerMeter = 0x0ec4; // paint and PSP use this values info.biYPelsPerMeter = 0x0ec4; info.biClrUsed = 0; // we are in RGB mode and have no palette info.biClrImportant = 0; // all colors are important // now we open the file to write to HANDLE file = CreateFile ( bmpfile , GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( file == NULL ) { CloseHandle ( file ); return FALSE; } // write file header unsigned long bwritten; if ( WriteFile ( file, &bmfh, sizeof ( BITMAPFILEHEADER ), &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // write infoheader if ( WriteFile ( file, &info, sizeof ( BITMAPINFOHEADER ), &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // write image data if ( WriteFile ( file, Buffer, paddedsize, &bwritten, NULL ) == FALSE ) { CloseHandle ( file ); return FALSE; } // and close file CloseHandle ( file ); return TRUE; } Ivan Cachicatari Blog[^] www.latindevelopers.com

          N Offline
          N Offline
          normanS
          wrote on last edited by
          #4

          Windows understands normal (upside down) bitmaps as well as "right-way-up" bitmaps - check the MSDN documentation on bitmap headers. As far as I remember, "right-way-up" bitmaps have negative height, so in your posted code, try changing:

          info.biHeight = height;

          to

          info.biHeight = -height;

          I use this technique with a StretchDIBits call, and it works there. Since you are not processing the bitmap data at all, whether this technique works will depend on the application you use to display the bitmap. A "well-behaved" application (conforming to MSDN bitmap standards) will display correctly. A "badly behaved" application will display the bitmap upside down or crash. Some applications may accept it, some may not! But it's easy to try.

          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