How to dump (write) IStream content to file (image)

I have an IStream that I know contains a PNG file, but I cannot write its contents to a file such as a normal I / O stream, I don't know if I am doing something wrong or if I am to write the IStream to the file should take another action.

    IStream *imageStream;
    std::wstring imageName;
    packager.ReadPackage(imageStream, &imageName);      

    std::ofstream test("mypic.png");
    test<< imageStream;

      

+3


source to share


1 answer


Based on the link IStream

you provided here is untested code that should do roughly what you want:



void output_image(IStream* imageStream, const std::string& file_name)
{
    std::ofstream ofs(file_name, std::ios::binary); // binary mode!!

    char buffer[1024]; // temporary transfer buffer

    ULONG pcbRead; // number of bytes actually read

    // keep going as long as read was successful and we have data to write
    while(imageStream->Read(buffer, sizeof(buffer), &pcbRead) == S_OK && pcbRead > 0)
    {
        ofs.write(buffer, pcbRead);
    }

    ofs.close();
}

      

+4


source







All Articles