How to get a Stream from in-memory Image?
I have an in-memory Image
, I need to g开发者_如何学Pythonet a Stream
from it to write into an OpenXML document. Can someone please point me to the correct method.
You can use an overload of the Save
method that takes a Stream
as a parameter. To create a stream that exists in-memory, you can use MemoryStream
type. Something like this should work:
// 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);
Now you can pass the newly created stream ms
to other objects (e.g. to save it in OpenXML
). See also:
- Image.Save method at MSDN
I'd fix a few things:
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
精彩评论