我们有在ASP.NET MVC3中上传图片的场景。WebImage和大内存消耗
控制器
public ActionResult Upload(IEnumerable<HttpPostedFileBase> images, SomeViewModel model) { foreach(var i in images) { ... byte[] fileBytes = i.InputStream.GetBytesArray(); byte[] image = _imageManager.Resize(fileBytes, MaxImageWidth, MaxImageHeight, true); ... } }
的ImageManager
public byte[] Resize(byte[] content, int width, int height, bool preserveAR = true) { if (content == null) return null; WebImage wi = new WebImage(content); wi = wi.Resize(width, height, preserveAspectRatio); return wi.GetBytes(); }
因此,我们收到来自客户端的图像作为HttpPostedFileBase。我们将byte [] fileBytes传递给imageManager的Resize方法。图像管理器正在创建新的WebImage实例,然后调整图像大小并将其再次转换为byte []。
调试此代码时,此刻我传递wi.GetBytes()行,我的内存使用率急剧上升(至少500mb)。我正在上传10mb的图片。上传较小尺寸的照片(约1.5mb)时,内存消耗正常。
这可能是什么原因造成的,这可以通过某种方式解决吗?
谢谢