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