将System.Windows.Xps.Packaging.XpsDocument对象转换为byte[]的最佳方法是什么?
发布于 2015-05-27 08:30:13
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);
}发布于 2015-05-27 08:08:18
您可以以这种方式转换大多数对象,XpsDocument也可以:
BinaryFormatter binaryFormatter = new BinaryFormatter();
using(MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, anyObject);
var result=ms.ToArray();
}https://stackoverflow.com/questions/30476560
复制相似问题