#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifndef WIN32
#define BI_RGB 0L
#pragma pack(1)
typedef struct tagBITMAPINFOHEADER{
unsigned long biSize;
long biWidth;
long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
} BITMAPINFOHEADER;//, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
typedef struct tagBITMAPFILEHEADER {
unsigned short bfType;
unsigned long bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned long bfOffBits;
} BITMAPFILEHEADER;//, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;
#pragma pack()
#endif
#define WIDTH 800
#define HEIGHT 600
int main()
{
BITMAPINFOHEADER bmih;
BITMAPFILEHEADER bmfh;
char cData[WIDTH * HEIGHT * 3];
FILE *pf;
int i;
int j;
int k = 0;
srand((unsigned int)time(0));
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biWidth = WIDTH;
bmih.biHeight = HEIGHT;
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biCompression = BI_RGB;
bmih.biSizeImage = bmih.biWidth * bmih.biHeight * 3;
bmih.biXPelsPerMeter = 0;
bmih.biYPelsPerMeter = 0;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
bmfh.bfType = 0x4d42;
bmfh.bfSize = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + bmih.biSizeImage;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
for(i = 0; i < HEIGHT; ++i){
for(j = 0; j < WIDTH; ++j){
cData[k++] = (char)(rand() % 255);
cData[k++] = (char)(rand() % 255);
cData[k++] = (char)(rand() % 255);
}
}
pf = fopen("bitmap.bmp", "wb");
if(!pf){
printf("open file failed\n");
return -1;
}
fwrite((const char *)&bmfh, sizeof(BITMAPFILEHEADER), 1, pf);
fwrite((const char *)&bmih, sizeof(BITMAPINFOHEADER), 1, pf);
fwrite((const char *)cData, sizeof(cData), 1, pf);
fclose(pf);
return 0;
}
bmp文件源码
最新推荐文章于 2021-05-20 12:13:06 发布