首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将XpsDocument转换为Byte[]

如何将XpsDocument转换为Byte[]
EN

Stack Overflow用户
提问于 2015-05-27 08:04:14
回答 2查看 890关注 0票数 1

System.Windows.Xps.Packaging.XpsDocument对象转换为byte[]的最佳方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-27 08:30:13

代码语言:javascript
复制
public static byte[] GenerateByteArrayFromXpsDocument()
{
    string tempFileName = System.IO.Path.GetTempFileName();

    //GetTempFileName creates a file, the XpsDocument throws an exception if the file already
    //exists, so delete it. Possible race condition if someone else calls GetTempFileName
    File.Delete(tempFileName);

    using (XpsDocument xpsDocument = new XpsDocument(tempFileName, FileAccess.ReadWrite))
    {
         XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
         writer.Write(/* use my own way to write xps file */);  // use your own way to write write the xps file instead
    }

    return File.ReadAllBytes(tempFileName);
}
票数 0
EN

Stack Overflow用户

发布于 2015-05-27 08:08:18

您可以以这种方式转换大多数对象,XpsDocument也可以:

代码语言:javascript
复制
BinaryFormatter binaryFormatter = new BinaryFormatter();
using(MemoryStream memoryStream = new MemoryStream())
{
    binaryFormatter.Serialize(memoryStream, anyObject);
    var result=ms.ToArray();
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30476560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档