用程序生成bmp图片

用程序生成bmp图片
 
 
纯手写bmp图片一文中,我们用16进制编辑器纯手工写出了一个16*16大小的24位色的bmp图片,通过文章的分析,你会发现,只要正确填充了bmp文件的头部,就能够显示出一幅bmp图片,虽然图片的内容看起来可能毫无意义。纯手工写bmp图片时,因为各种计算和输入的错误,很可能导致写出的图片不正确,这种做起来效率也很低,很麻烦,今天,我们就在此基础上,用程序生成一个任意大小的bmp图片。

关于bmp图片的各个结构的分析,在 纯手写bmp图片一文中讲的很详细, 在此,不再重复,这里,只给出本人写的生成bmp图片的源代码:


// CreateBmp24.c


#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;
    int heigth;

    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;
    struct tm* pnow;
    HANDLE hFile;
    BYTE *pData = NULL;
    int i, num;
    BITMAPFILEHEADER bmfHdr;
    BITMAPINFOHEADER bi;
    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

    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);
    pnow = localtime(&ltime);
    wsprintf(szFileName, "C://%04d%02d%02d-%02d%02d%02d.bmp", pnow->tm_year, 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 )
    {
        free(pData);
        return;
    }

    // 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);
}


代码中有比较详细的注释,而且也不难,所以,就不再详细说明了。

用VC6新建立一个Win32 Console Application,选择空工程,然后把上述文件加入,编译一下,在命令行运行,比如:
C:/> CreateBmp24.exe 256 256
你就会得到一个256*256大小的24色位图,文件默认保存在C:/下面,文件名是根据当前的日期和时间自动生成的。

Author: thinker
QQ: 94483026
E-Mail:cnhnyu<AT>gmail<DOT>com

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值