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

点点圈 提交于 2019-12-01 10:56:01

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

You need to specify the size of the palette that you attached. Right now it's zero, so the palette is showing up as the first bunch of pixels in your image.

bmi.bmiHeader.biClrUsed = 256;

You need to generate a palette for your image. Each pixel in a 32bit image is stored as 8-bits for Alpha, Red, Green and Blue. Where as in a 8bit image, the value in each pixel an 8bit index into the palette.

Your for(i=0..255) { bmi.bmiColors[i].rgbRed = i; ....} code is generated an grey-scale palette.

If the whole image is coming out as grey then it sounds like an alignment error, from memory the width of a palettized image must be a multiple of 4.

Try saving a SMALL 256 colour (aka 8-bit image) from Paint and compare in a hex editor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!