Creating 8bpp bitmap with GDI and saving it as a file

后端 未结 3 1041
礼貌的吻别
礼貌的吻别 2021-01-15 06:46

I have a perfectly working code that creates 32bpp bitmap and I need to change it so that 8bpp bitmap is created.

Here\'s the piece of code that creates 32bpp bitmap,

3条回答
  •  醉梦人生
    2021-01-15 07:04

    It looks like an alignment problem. Make sure you update bfOffBits in the BITMAPFILEHEADER so that it points to the first byte of the pixel data. (If you don't change it, then it probably points to the beginning of the palette.)

    In other words, sizeof(RGBQUAD)*256 should be added here as well:

    bf.bfOffBits = sizeof(BITMAPFILEHEADER) + bmi.bmiHeader.biSize;
    

    Also makes sure the first scanline starts on a DWORD boundary. That is, its offset from the beginning of the file should be a multiple of four bytes. Likewise, each scanline should be padded out to a multiple of four bytes. (You may not see these problems if your widths are nice even numbers. It's good to have an odd-width image among your test cases.)

提交回复
热议问题