流式传输到图像并返回

发布于 2024-12-06 13:26:32 字数 493 浏览 2 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2024-12-13 13:26:32

你基本上想要这样的东西,不是吗:

public void Resize(Stream input, Stream output, int width, int height)
{
    using (var image = Image.FromStream(input))
    using (var bmp = new Bitmap(width, height))
    using (var gr = Graphics.FromImage(bmp))
    {
        gr.CompositingQuality = CompositingQuality.HighSpeed;
        gr.SmoothingMode = SmoothingMode.HighSpeed;
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gr.DrawImage(image, new Rectangle(0, 0, width, height));
        bmp.Save(output, ImageFormat.Png);
    }
}

它将像这样使用:

using (var input = File.OpenRead("input.jpg"))
using (var output = File.Create("output.png"))
{
    Resize(input, output, 640, 480);
}

You basically want something like this, don't you:

public void Resize(Stream input, Stream output, int width, int height)
{
    using (var image = Image.FromStream(input))
    using (var bmp = new Bitmap(width, height))
    using (var gr = Graphics.FromImage(bmp))
    {
        gr.CompositingQuality = CompositingQuality.HighSpeed;
        gr.SmoothingMode = SmoothingMode.HighSpeed;
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gr.DrawImage(image, new Rectangle(0, 0, width, height));
        bmp.Save(output, ImageFormat.Png);
    }
}

which will be used like this:

using (var input = File.OpenRead("input.jpg"))
using (var output = File.Create("output.png"))
{
    Resize(input, output, 640, 480);
}
不必了 2024-12-13 13:26:32

这看起来很简单。您必须读取整个图像内容才能对其进行处理,并且必须将结果写回。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文