如何读入位图(三)

本文介绍了一个使用C语言实现的函数,用于从BMP文件中读取信息头的各项参数,包括宽度、高度、位数等。该函数通过打开文件、跳过文件头并逐个读取信息头中的字段来完成任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C语言读入信息头:

int ReadInfoHeader(char *filepath,BITMAPINFOHEADER *bmih)

{

       FILE *fp;

       //打开文件

       fp=fopen(filepath,"rb");

       if(!fp)

       {

              printf("Can not open the file:%s\n",filepath);

              return -1;

       }

       //使文件指针跳过文件头(14字节)

       fseek(fp,14,SEEK_SET);

       //读入biSize

       if(fread(&bmih->biSize,sizeof(DWORD),1,fp)!=1)

       {

              printf("Can not read biSize in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biWidth

       if(fread(&bmih->biWidth,sizeof(LONG),1,fp)!=1)

       {

              printf("Can not read biWidth in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biHeight

       if(fread(&bmih->biHeight,sizeof(LONG),1,fp)!=1)

       {

              printf("Can not read biHeight in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biPlanes

       if(fread(&bmih->biPlanes,sizeof(WORD),1,fp)!=1)

       {

              printf("Can not read biPlanes in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biBitCount

       if(fread(&bmih->biBitCount,sizeof(WORD),1,fp)!=1)

       {

              printf("Can not read biBitCount in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biCompression

       if(fread(&bmih->biCompression,sizeof(DWORD),1,fp)!=1)

       {

              printf("Can not read biCompression in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biSizeImage

       if(fread(&bmih->biSizeImage,sizeof(DWORD),1,fp)!=1)

       {

              printf("Can not read biSizeImage in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biXPelsPerMeter

       if(fread(&bmih->biXPelsPerMeter,sizeof(LONG),1,fp)!=1)

       {

              printf("Can not read biXPelsPerMeter in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biYPelsPerMeter

       if(fread(&bmih->biYPelsPerMeter,sizeof(LONG),1,fp)!=1)

       {

              printf("Can not read biYPelsPerMeter in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biClrUsed

       if(fread(&bmih->biClrUsed,sizeof(DWORD),1,fp)!=1)

       {

              printf("Can not read biClrUsed in the info header.\n");

              fclose(fp);

              return -1;

       }

       //读入biClrImportant

       if(fread(&bmih->biClrImportant,sizeof(DWORD),1,fp)!=1)

       {

              printf("Can not read biClrImportant in the info header.\n");

              fclose(fp);

              return -1;

       }

       //关闭文件

       fclose(fp);

 

       return 0;

}

转载于:https://www.cnblogs.com/djcsch2001/archive/2011/02/21/1960202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值