帮我写一份用C语言打开图片文件的代码
时间: 2023-05-21 19:01:19 浏览: 174
以下是一份用C语言打开图片文件的代码:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char filename[] = "example.jpg";
unsigned char *image;
int width, height;
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Failed to open file: %s\n", filename);
return 1;
}
fseek(fp, 0x0012, SEEK_SET);
fread(&width, sizeof(int), 1, fp);
fread(&height, sizeof(int), 1, fp);
fseek(fp, 0x0036, SEEK_SET);
image = (unsigned char*) malloc(width * height * 3);
fread(image, sizeof(unsigned char), width * height * 3, fp);
fclose(fp);
printf("Image width: %d\n", width);
printf("Image height: %d\n", height);
free(image);
return 0;
}
```
这份代码可以打开一个名为 "example.jpg" 的图片文件,并读取它的宽度和高度。注意,这份代码只适用于读取 JPEG 格式的图片文件。