I watched the solution of this problem on msdn and here: C ++ How to create a bitmap file

It creates a file with the extension .bmp but there is nothing in the description, the file says that it is damaged. read the title, looked like that. tried to copy from bmp to bmp, if you completely copy bmpFileHeader and bmpInfoHeader then it works. Also tried to do some more manipulations, flip flip. Looks like I'm doing something wrong with the headlines.

BITMAPFILEHEADER bmpFileHeader = { 0 }; BITMAPINFOHEADER bmpInfoHeader = { 0 }; RGBQUAD RGBcolor; for (size_t i = 0; i < height*width; ++i) image[i].reserved = 0; bmpFileHeader.bfType = 0x4d42; bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfoHeader.biWidth = width; bmpInfoHeader.biHeight = height; bmpInfoHeader.biSizeImage = (DWORD)(bmpInfoHeader.biWidth * bmpInfoHeader.biHeight); bmpInfoHeader.biPlanes = 1; bmpInfoHeader.biBitCount = 8; bmpInfoHeader.biCompression = BI_RGB; bmpInfoHeader.biXPelsPerMeter = bmpInfoHeader.biYPelsPerMeter = 2000; bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER)+bmpInfoHeader.biSize + bmpInfoHeader.biSizeImage; bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + bmpInfoHeader.biSize + sizeof(RGBQUAD); for (size_t i = 0; i < height * width; ++i){ fwrite(&image[i], sizeof(RGBQUAD), 1, output); } typedef struct RGB{ BYTE rgbtBlue; BYTE rgbtGreen; BYTE rgbtRed; BYTE reserved; }; 
  • Have you tried to compare BMP with a worker? - Vladimir Martyanov
  • @ Vladimir Martianov tried. changed biBitCount to 16 and writes something, but distorts colors strongly and writes one color to 3 pixels ... - ParanoidPanda

1 answer 1

bmpFileHeader.bfSize - the field is incorrect, must be: bmpFileHeader.bfSize = sizeof(BITMAPINFOHEADER);

bmpFileHeader.bfOffBits - here you leave space for 256 palette entries - it's unclear why, because obviously you don’t need a palette here and you don’t fill it up, but write pixels right away.

Also note that the image in bmp is inverted, i.e. the bottom row of pixels comes first, then the one above it, and so on.

In general, you need to carefully read the description of each field. Your code shows that you missed a lot. Here is a description of the structure in Russian: http://www.vsokovikov.narod.ru/New_MSDN_API/Bitmaps/str_bitmapinfoheader.htm

As an example of a working code I can give my own: https://github.com/nzeemin/ukncbtl/blob/master/util/BitmapFile.cpp - the BmpFile_SaveScreenshot () function at the very beginning of the file.

Check the biBitCount field - now it shows you that the color is 8-bit, and you biBitCount in 32-bit pixels.

  • EMNIP, you can specify a negative height, then the lines of the image can be written in direct order. - insolor
  • I redid the title record. But he still writes a color of three pixels - ParanoidPanda
  • Looked at your code in more detail. Redid everything under 8 bits. and instead of RGBTriple I use RGBQUAD. read the spec format. in general, it just fills me now with one color ... Now I think to dig in to write to the file. - ParanoidPanda
  • @ParanoidPanda, update your code in the first message of the topic - then you can see what is wrong now. - nzeemin
  • @nzeemin updated - ParanoidPanda