这是我的代码,
#include<stdio.h>
#include<stdlib.h>
typedef struct tagBITMAPFILEHEADER
{
unsigned short bfType;
unsigned long bfSize;
unsigned short bfReserved1;
unsigned short brReserved2;
unsigned long bfbfOffBits;
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
unsigned long biSize;
    long biWidth;
long biHeight;
unsigned short biPlanes;
unsigned short biBitcount;
unsigned short biCompression;
unsigned long biSizeImage;
long biXPelsPermeter;
long biYPelsPermeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
}BITMAPINFOHEADER;
typedef struct
{
BITMAPFILEHEADER file;
BITMAPINFOHEADER info;
}bmp;bmp readbmpfile( void );int main()
{
bmp m;
m = readbmpfile();
// getchar();
return 0;
}bmp readbmpfile( void )
{
bmp m;
FILE *fp;
if( (fp = fopen( "d:\\1.bmp","r" )) == NULL )
{
printf( "cann't open the bmp image.\n" );
exit(0);
} else
{
fread(&m.file,1,sizeof(BITMAPFILEHEADER),fp ); 
        printf( "类型为:%x\n",m.file.bfType );
printf( "文件长度为:%x\n",m.file.bfSize );
printf( "保留字1为:%x\n",m.file.bfReserved1 );
printf( "保留字2为:%x\n",m.file.brReserved2 );
        printf( "偏移量为%x\n",m.file.bfbfOffBits );
fread( &m.info,1,sizeof(BITMAPFILEHEADER),fp );
printf( "此结构大小为:%x\n",m.info.biSize );
    printf( "位图的宽度为%x\n",m.info.biWidth );
        printf( "位图的高度为:%x\n",m.info.biHeight );
        printf( "目标设备位图数:%x\n",m.info.biPlanes );
        printf( "颜色深度为:%x\n",m.info.biBitcount );
        printf( "压缩类型为:%x\n",m.info.biCompression );
        printf( "位图大小:%x\n" );
        printf( "位图水平分辨率为:%x\n",m.info.biXPelsPermeter );
        printf( "位图的垂直分辨率为:%x\n",m.info.biYPelsPermeter );
        printf( "位图实际使用颜色数:%x\n",m.info.biClrUsed );
        printf( "位图显示中比较重要颜色数:%x\n",m.info.biClrImportant );
}
return m;
}输出就有问题了,
这是输出的部分结果
类型为:4d42
文件长度为:0
保留字1为:0
保留字2为:436
问什么文件长度会是0???明明bfsize定义的是unsigned long 类型的啊
希望哪位给解决一下   多谢了

解决方案 »

  1.   

    bfSize 
    Specifies the size, in bytes, of the bitmap file. 
    是不是文件长度很小的原因,换个大的图片
      

  2.   

    typedef struct tagBITMAPFILEHEADER{ 
    WORD bfType; // 位图文件的类型,必须为BM 
    DWORD bfSize; // 位图文件的大小,以字节为单位 
    WORD bfReserved1; // 位图文件保留字,必须为0 
    WORD bfReserved2; // 位图文件保留字,必须为0 
    DWORD bfOffBits; // 位图数据的起始位置,以相对于位图文件头的偏移量表示,以字节为单位 
    } BITMAPFILEHEADER;unsigned short bfType;这句错得太严重了