如何从内存映像中获取流?

时间:2010-06-21 09:34:09

标签: .net image stream openxml

我有一个内存中的Image,我需要从中获取Stream来写入OpenXML文档。有人可以指出我正确的方法。

2 个答案:

答案 0 :(得分:4)

您可以使用以Save作为参数的Stream方法的重载。要创建内存中存在的流,可以使用MemoryStream类型。这样的事情应该有效:

// Create new memory stream and save the image
var ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);

// Seek to the first byte in the stream, so that other classes who
// use it will start reading from the beginning
ms.Seek(0, SeekOrgin.Begin);

现在,您可以将新创建​​的流ms传递给其他对象(例如,将其保存在OpenXML中)。另见:

答案 1 :(得分:1)

我会解决一些问题:

var ms = new MemoryStream();
image.Save(ms, image.RawFormat); // Save it in the original format of the image
ms.Seek(0, SeekOrigin.Begin); // Fixed typo
相关问题