如何读入位图(三)

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/p/1960202.html