用c语言写一个读取bmp
时间: 2024-05-11 15:14:02 浏览: 18
文件并输出其基本信息的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#pragma pack(1) //按照1字节对齐
typedef struct {
unsigned short bfType; //文件类型,必须是0x424D,即BM
unsigned int bfSize; //文件大小,单位为字节
unsigned short bfReserved1; //保留字段1,必须为0
unsigned short bfReserved2; //保留字段2,必须为0
unsigned int bfOffBits; //位图数据的起始位置,单位为字节
} BMPFILEHEADER;
typedef struct {
unsigned int biSize; //信息头大小,必须为40
int biWidth; //位图宽度,单位为像素
int biHeight; //位图高度,单位为像素
unsigned short biPlanes; //位图平面数,必须为1
unsigned short biBitCount; //每像素位数,必须为1、4、8、16、24、32
unsigned int biCompression; //压缩方式,0为不压缩
unsigned int biSizeImage; //位图数据大小,单位为字节
int biXPelsPerMeter; //水平分辨率,单位为像素/米
int biYPelsPerMeter; //垂直分辨率,单位为像素/米
unsigned int biClrUsed; //使用的颜色数,如果是0,则使用所有颜色
unsigned int biClrImportant; //重要的颜色数,如果是0,则所有颜色都重要
} BMPINFOHEADER;
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s bmp_file\n", argv[0]);
return 0;
}
FILE *fp = fopen(argv[1], "rb");
if (!fp) {
printf("Can't open file %s\n", argv[1]);
return 0;
}
BMPFILEHEADER fileHeader;
BMPINFOHEADER infoHeader;
fread(&fileHeader, sizeof(fileHeader), 1, fp);
fread(&infoHeader, sizeof(infoHeader), 1, fp);
printf("File Header:\n");
printf("bfType: 0x%X\n", fileHeader.bfType);
printf("bfSize: %u\n", fileHeader.bfSize);
printf("bfReserved1: %u\n", fileHeader.bfReserved1);
printf("bfReserved2: %u\n", fileHeader.bfReserved2);
printf("bfOffBits: %u\n", fileHeader.bfOffBits);
printf("Info Header:\n");
printf("biSize: %u\n", infoHeader.biSize);
printf("biWidth: %d\n", infoHeader.biWidth);
printf("biHeight: %d\n", infoHeader.biHeight);
printf("biPlanes: %u\n", infoHeader.biPlanes);
printf("biBitCount: %u\n", infoHeader.biBitCount);
printf("biCompression: %u\n", infoHeader.biCompression);
printf("biSizeImage: %u\n", infoHeader.biSizeImage);
printf("biXPelsPerMeter: %d\n", infoHeader.biXPelsPerMeter);
printf("biYPelsPerMeter: %d\n", infoHeader.biYPelsPerMeter);
printf("biClrUsed: %u\n", infoHeader.biClrUsed);
printf("biClrImportant: %u\n", infoHeader.biClrImportant);
fclose(fp);
return 0;
}
```
该程序通过打开指定的bmp文件,读取文件头和信息头,并输出其基本信息。需要注意的是,在读取文件头和信息头时,需要按照1字节对齐,否则可能会出现不正确的数据。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)