能生成BMP http://bbs.bccn.net/thread-336164-1-1.html 为何该程序生成的BMP图像打不开?? 程序代码: #include <stdio.h> #include <time.h> #include <stdlib.h> #include <malloc.h> #include <conio.h> #include <pshpack2.h> #include <poppack.h> typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned long DWORD; /*定义位图文件头*/ typedef struct tag_BIT_MAP_FILE_HEADER { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BIT_MAP_FILE_HEADER; /*定义信息头*/ typedef struct tagBIT_MAP_INFO_HEADER { DWORD biSize; DWORD biWidth; DWORD biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; DWORD biXPelsPerMeter; DWORD biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BIT_MAP_INFO_HEADER; /*调色板*/ typedef struct tagRGB { BYTE blue; BYTE green; BYTE red; } RGB; int main() { RGB *bmp_data = NULL; FILE *bmp_file; long i; long width = 80; long height = 80; long date_size = width*height; BIT_MAP_FILE_HEADER bmp_map_file; BIT_MAP_INFO_HEADER bmp_map_info; if((bmp_file=fopen("cubs.bmp", "wb+")) == NULL) { printf("Error!\n"); system("pause"); exit(0); } /*写入图位文件头*/ bmp_map_file.bfType = 0x4d42; bmp_map_file.bfSize = 14 + 40 + width * height * 3; bmp_map_file.bfReserved1 = 0; bmp_map_file.bfReserved2 = 0; bmp_map_file.bfOffBits = 0x36; fwrite(&bmp_map_file, sizeof(BIT_MAP_FILE_HEADER), 1, bmp_file); /*写入信息头*/ bmp_map_info.biSize = 40; bmp_map_info.biPlanes = 1; bmp_map_info.biHeight = height; bmp_map_info.biWidth = width; bmp_map_info.biBitCount = 24; bmp_map_info.biClrImportant = 0; bmp_map_info.biClrUsed = 0; bmp_map_info.biSizeImage = width*height*3; bmp_map_info.biCompression = 0; bmp_map_info.biXPelsPerMeter = 0; bmp_map_info.biYPelsPerMeter = 0; fwrite(&bmp_map_info, sizeof(BIT_MAP_INFO_HEADER), 1, bmp_file); /*给图像分配内存*/ if((bmp_data=(RGB*)malloc(width * height * 3)) == NULL) { printf("申请内存失败!\n"); exit(0); } /*RGB赋值并写入文件中*/ for(i= 0; i<width*height; i++) { bmp_data[i].green=255; bmp_data[i].red=0; bmp_data[i].blue=0; } fwrite(bmp_data, sizeof(RGB), date_size, bmp_file); printf("BMP图像生成成功!\n\n\n"); fclose(bmp_file); free(bmp_data); bmp_data = NULL; return 0; } //============================================================================================ #pragma pack(push) #pragma pack(1) /*定义位图文件头*/ typedef struct tag_BIT_MAP_FILE_HEADER { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BIT_MAP_FILE_HEADER; /*定义信息头*/ typedef struct tagBIT_MAP_INFO_HEADER { DWORD biSize; DWORD biWidth; DWORD biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; DWORD biXPelsPerMeter; DWORD biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BIT_MAP_INFO_HEADER; /*调色板*/ typedef struct tagRGB { BYTE blue; BYTE green; BYTE red; } RGB; #pragma pack(pop) 如果你用的是VC,这样改一下再编译就行了
标签:info,map,MAP,bmp,生成,BMP,file,DWORD From: https://www.cnblogs.com/roak/p/16937996.html