【问题标题】:How to dump (write) IStream's content into file (Image)如何将 IStream 的内容转储(写入)到文件中(图片)
【发布时间】:2014-09-20 00:17:52
【问题描述】:

我有一个IStream,我知道它包含一个 PNG 文件,但我无法将其内容写入像普通 I/O 流这样的文件,我不知道我做错了什么还是应该为将IStream 写入文件做不同的事情。

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

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

【问题讨论】:

  • 图片数据需要使用二进制写入方式。 &lt;&lt; 会损坏某些系统中的文件。试试std::ofstream test("mypic.png", std::ios::binary); test.write(...);我不知道IStream()是怎么填空的。

标签: c++ c istream


【解决方案1】:

根据您在此处提供的IStream 参考,是一些未经测试的代码,应该可以大致完成您想要的操作:

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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多