#include<stdio.h>#include<stdlib.h>#definew200#defineh200voidWriteBMP(char*img,constchar*filename){int l =(w *3+3)/4*4;
FILE *fp =fopen(filename,"wb");fprintf(fp,"BM");int bmi[]={l * h +54,0,54,40, w, h,1|3*8<<16,0, l * h,0,0,100,0};fwrite(&bmi,52,1, fp);fwrite(img,1, l * h, fp);fclose(fp);}intmain(){char img[w * h *3];for(int i =0; i < w * h *3; i++)
img[i]=rand()%256;WriteBMP(img,"test.bmp");system("test.bmp");return0;}
结果:生成名为test.bmp图片
更加符合规格的bmp生成方法,
#include<stdio.h>#include<windows.h>#include<time.h>voidCreateBmp24(unsignedint width,unsignedint height);intmain(int argc,char**argv){int width=0x100;int heigth=0x100;// if ( argc != 3)// {// printf("Usage %s width height\n", argv[0]);// printf("Example:\n\t%s 16 16\n", argv[0]);// return -1;// }// // get the width and height from the command line// width = atoi(argv[1]);// heigth = atoi(argv[2]);CreateBmp24(width, heigth);return0;}voidCreateBmp24(unsignedint width,unsignedint height){char szFileName[32];time_t ltime;
HANDLE hFile;
BYTE *pData =NULL;int i, num;
DWORD dwBytesWrite;//文件数据if( width <=0|| height <=0)return;// allocate memory for data area
pData =(BYTE*)malloc(width * height *3);if( pData ==NULL)return;// generate random datasrand((unsigned)time(NULL));for( i =0; i <(int)(width * height *3); i++){do{
num =rand();// generate number between 0 and 255}while(num <0|| num >255);
pData[i]= num;}// initialize the structures
BITMAPFILEHEADER bmfHdr;// bmf文件头头
BITMAPINFOHEADER bi;//信息头RtlZeroMemory(&bmfHdr,sizeof(bmfHdr));RtlZeroMemory(&bi,sizeof(bi));// fill the necessary struct fields of the BITMAPFILEHEADER
bmfHdr.bfType =0x4d42;// "BM"
bmfHdr.bfSize =sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ width * height *3;
bmfHdr.bfOffBits =sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);// fill the necessary struct fields of the BITMAPINFOHEADER
bi.biSize =sizeof(bi);
bi.biBitCount =24;
bi.biHeight = height;
bi.biWidth = width;
bi.biCompression = BI_RGB;
bi.biPlanes =1;//文件名初始化/* according current date and time to generate file name, saved in c:/ */time(<ime);structtm* pnow =localtime(<ime);wsprintf(szFileName,"%04d%02d%02d-%02d%02d%02d.bmp",1900+pnow->tm_year,1+pnow->tm_mon, pnow->tm_mday,
pnow->tm_hour, pnow->tm_min, pnow->tm_sec);// create the bmp file
hFile =CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE,0,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);if( hFile != INVALID_HANDLE_VALUE ){// write the BITMAPFILEHEADERWriteFile(hFile,&bmfHdr,sizeof(BITMAPFILEHEADER),&dwBytesWrite,NULL);// write the BITMAPINFOHEADERWriteFile(hFile,&bi,sizeof(BITMAPINFOHEADER),&dwBytesWrite,NULL);// write the bmp dataWriteFile(hFile, pData, width * height *3,&dwBytesWrite,NULL);// release resourcesCloseHandle(hFile);}free(pData);printf("The bmp file is %s/n", szFileName);}
结果:生成20240222-155159.bmp类似名称的图片
生成有序图片的方法
#include<stdio.h>#include<windows.h>#include<time.h>#defineBMP_W0x100#defineBMP_H0x100#defineCOLOR_POST4// 3色和4色(带透明)voidCreateBmp24(constchar*filename){// if ( BMP_W == 0 || BMP_H == 0 )return;
BITMAPFILEHEADER BMP_0;// bmf文件头头
BITMAPINFOHEADER BMP_1;// 信息头
BMP_0.bfType =0x4d42;// "BM"
BMP_0.bfSize =sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ BMP_W * BMP_H *(COLOR_POST);
BMP_0.bfOffBits =sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
BMP_1.biSize =sizeof(BMP_1);// BMP_1.biBitCount = 24;
BMP_1.biBitCount = COLOR_POST *8;
BMP_1.biHeight = BMP_H;
BMP_1.biWidth = BMP_W;
BMP_1.biCompression = BI_RGB;
BMP_1.biPlanes =1;
BMP_1.biClrUsed =16;// 索引表数据// allocate memory for data area// unsigned char *BMP_DATA = malloc(BMP_W * BMP_H * 3);unsignedchar BMP_DATA[BMP_W][BMP_H][COLOR_POST];printf("size1:%d\r\n",sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER));printf("size2:%d\r\n", BMP_W * BMP_H * COLOR_POST);if(BMP_DATA ==NULL)return;int index =0;for(int ix =0; ix < BMP_W; ix++){for(int iy =0; iy < BMP_H; iy++){// for (int iz = 0; iz < BMP_H ; iz++){
BMP_DATA[iy][ix][0]=(unsignedchar)ix /16*16;// 蓝色
BMP_DATA[iy][ix][1]=(unsignedchar)iy /16*16;// 蓝色
BMP_DATA[iy][ix][2]=(unsignedchar)iy /16*16;// }}}// 当前时间/* according current date and time to generate file name, saved in c:/ */// create the bmp file
FILE *file =NULL;fopen_s(&file, filename,"ab+");// 用r会出现读取不全,用rb数据不够准确if(file !=NULL){fseek(file,0,0);fwrite(&BMP_0,sizeof(BITMAPFILEHEADER),1, file);fseek(file,sizeof(BITMAPFILEHEADER),0);fwrite(&BMP_1,sizeof(BITMAPINFOHEADER),1, file);fseek(file,sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER),0);fwrite(BMP_DATA,1, BMP_W * BMP_H * COLOR_POST, file);fclose(file);// 以只读方式打开,不需要写入printf("The bmp file is %s", filename);
file =NULL;}// free(BMP_DATA);}intmain(int argc,char**argv){time_t ltime;time(<ime);structtm*pnow =localtime(<ime);// 文件名初始化char filename[32];wsprintf(filename,"%04d%02d%02d-%02d%02d%02d.bmp",1900+ pnow->tm_year,1+ pnow->tm_mon, pnow->tm_mday,
pnow->tm_hour, pnow->tm_min, pnow->tm_sec);CreateBmp24(filename);return0;}