【一个使用调色板的3色位图的例子】
#include <stdio.h>
#include <Windows.h>
#pragma warning(disable: 4996)
int main(void)
{
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
BYTE bits[100][76]; // 150/2=75, 76%4=0
RGBQUAD colors[] = {{201, 174, 254, 0}, {10, 166, 84, 0}, {1, 128, 255, 0}};
FILE *fp;
int i;
fp = fopen("image.bmp", "wb");
if (fp == NULL)
return 1;
fileheader.bfType = *(PWORD)"BM";
fileheader.bfSize = sizeof(fileheader) + sizeof(infoheader) + sizeof(colors) + sizeof(bits);
fileheader.bfReserved1 = fileheader.bfReserved2 = 0;
fileheader.bfOffBits = sizeof(fileheader) + sizeof(infoheader) + sizeof(colors);
fwrite(&fileheader, sizeof(fileheader), 1, fp);
infoheader.biSize = sizeof(infoheader);
infoheader.biWidth = 150;
infoheader.biHeight = 100;
infoheader.biPlanes = 1;
infoheader.biBitCount = 4; // 1~16色
infoheader.biCompression = BI_RGB;
infoheader.biSizeImage = 0;
infoheader.biXPelsPerMeter = infoheader.biYPelsPerMeter = 0;
infoheader.biClrUsed = _countof(colors); // 颜色数
infoheader.biClrImportant = 0;
fwrite(&infoheader, sizeof(BITMAPINFOHEADER), 1, fp);
fwrite(&colors, sizeof(colors), 1, fp);
memset(bits, 0, sizeof(bits));
for (i = 0; i < 30; i++)
memset(bits[i], 0x11, 75);
for (; i < 70; i++)
memset(bits[i], 0x22, 75);
fwrite(bits, sizeof(bits), 1, fp);
fclose(fp);
return 0;
}
所创建的位图: