MemoryTributary GetBuffer

时间:2014-02-23 03:27:39

标签: c# asp.net-mvc out-of-memory

我的应用程序是MVC5 C#,我使用memorystream通过以下方式生成图像:

                  using (var memStream = new MemoryStream())
                    {
                        const int quality = 90; 
                        var encoderParameters = new EncoderParameters(1);
                        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, (long)quality);
                        objImage.Save(memStream, GetImageCodeInfo("image/png"), encoderParameters);
                       data = this.File(memStream.GetBuffer(), "image/png");
                        memStream.Dispose();

                      }

但是我得到一些文件的OutOfMemoryException。我正在阅读关于MemoryTributary的内容,但找不到GetBuffer的解决方案!非常感谢您的建议。

1 个答案:

答案 0 :(得分:0)

如何将所有缓冲区内容留下来?

var memStream = new MemoryStream();

const int quality = 90; 
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, (long)quality);
objImage.Save(memStream, GetImageCodeInfo("image/png"), encoderParameters);

memStream.Seek(0, SeekOrigin.Begin);

return File(memStream, "image/png");

FileStreamResult将部署MemoryStream,无需担心。