有没有办法在特定路径中获取 png 文件的尺寸?我不需要加载文件,我只需要宽度和高度来在 directx 中加载纹理。
(而且我不想使用任何第三方库)
最佳答案
宽度是一个 4 字节整数,从文件中的偏移量 16 开始。高度是另一个从偏移量 20 开始的 4 字节整数。它们都是按网络顺序排列的,因此您需要转换为主机顺序才能正确解释它们。
#include <fstream>
#include <iostream>
#include <winsock.h>
int main(int argc, char **argv) {
std::ifstream in(argv[1]);
unsigned int width, height;
in.seekg(16);
in.read((char *)&width, 4);
in.read((char *)&height, 4);
width = ntohl(width);
height = ntohl(height);
std::cout << argv[1] << " is " << width << " pixels wide and " << height << " pixels high.\n";
return 0;
}
关于C++如何获取png文件的图像大小(在目录中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5354459/