#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;
}
写了个程序生成bmp图像,可是发现打不开
最新推荐文章于 2021-05-25 12:45:33 发布