写了个程序生成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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值