C ++如何获取png文件的图像大小(在目录中) [英] C++ How to get the Image size of a png file (in directory)

查看:146
本文介绍了C ++如何获取png文件的图像大小(在目录中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以获得特定路径中png文件的尺寸?我不需要加载文件,我只需要宽度和高度来加载纹理在directx。

Is there a way of getting the dimensions of a png file in a specific path? I don´t need to load the file, I just need the width and height to load a texture in directx.

(我不想使用任何第三-party-libs)

(And I don´t want to use any third-party-libs)

推荐答案

宽度是一个4字节整数,从文件中的偏移16开始。高度是从偏移20开始的另一个4字节整数。它们都是网络顺序,因此您需要转换为主机顺序以正确解释它们。

The width is a 4-byte integer starting at offset 16 in the file. The height is another 4-byte integer starting at offset 20. They're both in network order, so you need to convert to host order to interpret them correctly.

#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文件的图像大小(在目录中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆