C ++ How to get the image size of a png file (in a directory)

Is there a way to get png file sizes in a specific path? I don’t need to upload the file, I just need the width and height to load the texture in directx.

(And I do not want to use third-party libraries)

+5
source share
5 answers

Width is a 4-byte integer starting at offset 16 in the file. Height is another 4-byte integer starting at offset 20. They are both in network order, so you need to convert to the host order in 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;
}
+8
source

, , . , , , .

, PNG , 16 20 ( 4 , -), , 8 "89 50 4E 47 0D 0A 1A 0A" (hex), 12-15 - "49 48 44 52" ( "IHDR" ASCII).

+6

, .

, , PNG .

+3

. PNG.. IHDR, . , .

+1

... . (PNG, GIF JPEG)

0

All Articles