Save Image into a file in C++

  c++, file, image, save, sfml

I want to save an image into a custom file, in order to secure all images of my program.

I tried to save every pixels (as Uint32) of an image like this (I am using SFML)

void fromPNGtoCustomFile(sf::Texture texture, std::string path)
    std::ofstream fo;;

    sf::Image image=texture.copyToImage(); //GET IMAGE OF THE TEXTURE

    fo << image.getSize().x << " " << image.getSize().y << " "; // WRITE THE SIZE OF THE IMAGE

    for(unsigned int i=0; i< image.getSize().x; i++)
        for(unsigned int j=0; j< image.getSize().y; j++)
            fo << image.getPixel(i, j).toInteger() << " "; 

            // image.getPixel(x, y).toInteger() RETURNS A Uint32 VALUE


Then I load image from the file using the same logic.

It worked, but I realised that the size of the file I created was around 250 Mb, when my original .png image was only 8 Mb. If I compress the new file using 7Zip, I get a 8 Mb file, as the original size. I do not understand why I get this size.

I don’t really know what is the best way to create custom file for saving images.

Do you have any suggestions, or correction of my code?

Source: Windows Questions C++