流式传输到图像并返回
我正在将 Stream
将其转换为 Image
,处理该图像,然后返回 FileStream
。
这是性能问题吗?如果不是,转换和返回流的优化方法是什么?
public FileStream ResizeImage(int h, int w, Stream stream)
{
var img = Image.FromStream(stream);
/* ..Processing.. */
//converting back to stream? is this right?
img.Save(stream, ImageFormat.Png);
return stream;
}
运行的情况:用户在我的网站上上传图像(控制器给我一个流,我调整它的大小,然后将此流发送到机架空间(Rackspace 需要一个 FileStream)。
I'm taking a Stream
convert it to Image
, process that image, then return a FileStream
.
Is this a performance problem? If not, whats the optimized way to convert and return back a stream?
public FileStream ResizeImage(int h, int w, Stream stream)
{
var img = Image.FromStream(stream);
/* ..Processing.. */
//converting back to stream? is this right?
img.Save(stream, ImageFormat.Png);
return stream;
}
The situation in which this is running: User uploads image on my site (controller gives me a Stream, i resize this, then send this stream to rackspace (Rackspace takes a FileStream
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你基本上想要这样的东西,不是吗:
它将像这样使用:
You basically want something like this, don't you:
which will be used like this:
这看起来很简单。您必须读取整个图像内容才能对其进行处理,并且必须将结果写回。
FileStreams 是处理文件的正常 .NET 方式,因此对于正常目的,您的方法是可以的。
我唯一不明白的是为什么你再次返回 FileStream - 它与参数传递的对象是同一个对象。
如果您正在处理大量图像并且仅修改部分数据,则内存映射文件可以提高性能。然而,它是一个更先进的概念。
That looks as simple as it can be. You have to read the entire image contents to be able to process it and you have to write the result back.
FileStreams are the normal .NET way to handle files, so for normal purposes your approach is okay.
The only thing I don't understand is why you return the FileStream again - it is the same object as was passed by a parameter.
If you are doing a lot of images and only modify parts of the data, memory mapped files could improve performance. However it is a more advanced concept to use.