【bmp图片生成】

文章介绍了三种C语言实现的BMP位图文件生成方法,包括基础的WriteBMP函数,更符合规范的CreateBmp24函数,以及创建有序图片的CreateBmp24版本,详细展示了位图文件头和信息头的设置过程。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>
#define w 200
#define h 200
void WriteBMP(char *img, const char *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);
}

int main()
{
    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");
    return 0;
}
  • 结果:生成名为test.bmp图片
    在这里插入图片描述

更加符合规格的bmp生成方法,

#include <stdio.h>
#include <windows.h>
#include <time.h>

void CreateBmp24(unsigned int width, unsigned int height);

int main(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);

    return 0;
}

void CreateBmp24(unsigned int width, unsigned int 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 data
    srand((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(&ltime);
    struct tm* pnow = localtime(&ltime);
    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 BITMAPFILEHEADER

        WriteFile(hFile, &bmfHdr, sizeof(BITMAPFILEHEADER), &dwBytesWrite, NULL);
        // write the BITMAPINFOHEADER

        WriteFile(hFile, &bi, sizeof(BITMAPINFOHEADER), &dwBytesWrite, NULL);
        // write the bmp data

        WriteFile(hFile, pData, width * height * 3, &dwBytesWrite, NULL);
        
        // release resources
        CloseHandle(hFile);
    }
    free(pData);
    printf("The bmp file is %s/n", szFileName);
}
  • 结果:生成20240222-155159.bmp类似名称的图片
    在这里插入图片描述

生成有序图片的方法

#include <stdio.h>
#include <windows.h>
#include <time.h>

#define BMP_W 0x100
#define BMP_H 0x100
#define COLOR_POST 4 // 3色和4色(带透明)

void CreateBmp24(const char*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);
    unsigned char 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] = (unsigned char)ix / 16 * 16; // 蓝色
            BMP_DATA[iy][ix][1] = (unsigned char)iy / 16 * 16; // 蓝色
            BMP_DATA[iy][ix][2] = (unsigned char)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);
}

int main(int argc, char **argv)
{
    time_t ltime;
    time(&ltime);
    struct tm *pnow = localtime(&ltime);
    // 文件名初始化
    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);
    return 0;
}

在这里插入图片描述

bmp文件格式说明

地址(hex)字节长度(byte)描述:位图文件头(14 bytes)
002固定头文件字段,内容为0x424D
024bmp文件大小(little endian)
062预留字段
082预留字段
0A4图片信息的开始位置
地址(hex)字节长度(byte)描述:位图信息数据头(40bytes)
0E4位图信息数据头的大小 40bytes
124图像宽度(little endian)
164图像高度(little endian)
1A2色彩平面的数量,默认为1
1C2每像素用多少bit表示
1E4图片采用的压缩方式,通常不压缩即BL_RGB,对应值0
224图片大小(原始位图数据大小)对于不压缩的图片,默认为0
264横向分辨率(像素/米)
2A4纵向分辨率(像素/米)
2E4调试板中颜色数量,默认为0
324重要颜色的数量,默认为0
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值