C++如何获取png文件的图像大小(在目录中)

标签 c++ image png

有没有办法在特定路径中获取 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/

相关文章:

c++ - 需要使上下文可用于 C++ ostream 插入运算符

html - 出现在同一背景图像下的下一页的标题

python - 如何在图像颜色为黑色时使用 Pillow 将 PNG 转换为 JPG?

c++ - 泛化模型中的数据访问。避免宏的方法?

c++ - 将字符串转换为 TDateTime 变量

java.io.Image 到 InputStream

Java Swing - 如何在Jpanel的北部添加图像

Jquery - PNG 图像中的 fadeIn() 和 fadeOut()。 IE8 中的边框为实心(黑色)...?

powershell - 使用变量从 PowerShell 打印 PNG 文件

c++ - malloc/new 在函数指针内失败