新手请教:c语言处理位图

来源:百度知道 编辑:UC知道 时间:2024/05/26 18:48:04
用这个简单的程序,读位图的文件头,但结果却和预期相差很大
#include "Conio.h"
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>

typedef unsigned short int WORD;
typedef unsigned long int DWORD;
typedef unsigned char BYTE ;

typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
DWORD biWidth;
DWORD biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
DWORD biXPelsPerMeter;
DWORD biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;

BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bmiHeader;

void

你没有把文件的内容读进结构体里面,当然结果不可预料
而且,如果你直接fread(&bmfHeader, sizeof(bmfHeader), 1, fp)也可能会出错,因为编译器在编译结构体的时候,为了加快访问,会进行结构体的对齐操作。

所以你要逐一字段地读取bmp图像的信息单元