bmp.h:
#define u8 unsigned char int BMP_HEAD_WIDTH[4] = { 18,19,20,21 }; //图片宽度(像素) int BMP_HEAD_HEIGH[4] = { 22,23,24,25 }; //图片高度(像素) u8 BMP_HEAD[54] = { 0x42,//B 0x4D,//M 0x46,//2 0x00,//3 0x00,//4 0x00,//5 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x02,//18 0x00,//19 0x00,//20 0x00,//21 0x02,//22 0x00,//23 0x00,//24 0x00,//25 0x01, 0x00, 0x18,//28 0x00,//29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//38 0x00,//39 0x00,//40 0x00,//41 0x00,//42 0x00,//43 0x00,//44 0x00,//45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
main.c
#define _CRT_SECURE_NO_WARNINGS #include#include "bmp.h" u8 bmp_data[2][4*3] = { { 0x00,0x00,0xff,0x00,0x6a,0xff,0x00,0x00,0x00,0x40,0x40,0x40}, //第二行 {0x00,0x00,0x00,0x40,0x40,0x40,0x00,0x00,0xff,0x00,0x6a,0xff} //第一行 }; void WriteRgb8882Bmp(char*filename, int heigh, int width) { int i = 0,j=0; int BmpHeadLeng = 54; FILE* fp = fopen(filename, "w+");//D://Users//wzx//Desktop// BMP_HEAD[BMP_HEAD_WIDTH[0]] = width & 0xff; BMP_HEAD[BMP_HEAD_WIDTH[1]] = width & 0xff00; BMP_HEAD[BMP_HEAD_WIDTH[2]] = width & 0xff0000; BMP_HEAD[BMP_HEAD_WIDTH[3]] = width & 0xff000000; BMP_HEAD[BMP_HEAD_HEIGH[0]] = heigh & 0xff; BMP_HEAD[BMP_HEAD_HEIGH[1]] = heigh & 0xff00; BMP_HEAD[BMP_HEAD_HEIGH[2]] = heigh & 0xff0000; BMP_HEAD[BMP_HEAD_HEIGH[3]] = heigh & 0xff000000; for (i = 0;i < 54;i++) { fprintf(fp,"%c",BMP_HEAD[i]); } int col_len = sizeof(bmp_data[0]); //列数一行的字节数 必须是4的倍数,每个像素点用3个字节表示 int row_len = sizeof(bmp_data)/col_len;//行数 for (i = 0;i < row_len;i++) { for (j = 0;j < col_len;j++) { fprintf(fp, "%c", bmp_data[i][j]); } //一行数据写完 if (col_len % 4 != 0) //一行的像素点数如果不是4的倍数,补0 { for (int ii = 0;ii < col_len % 4;ii++) { fprintf(fp, "%c", 0x00); } } } fclose(fp); } int main() { WriteRgb8882Bmp("D://Users//wzx//Desktop//result.bmp",2,4); }
使用paint.net打开2行4列
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)