我在 MVC3 中使用 WebImage
来调整图像的大小。基本上,这样做的目的是创建上传文件的缩略图。我将无法控制文件的原始大小,因此我需要创建文件的缩略图以加快“预览”站点的速度。
我有一些文件需要上传,大小约为 4Mb,这在上传时不是问题。我遇到的问题是创建缩略图。我先上传文件,一旦它保存在服务器上,我就会为缩略图创建一个新的 WebImage
对象。
// Save a thumbnail of the file
WebImage image = new WebImage(savedFileName);
// Resize the image
image.Resize(135, 150, true);
// Save the thumbnail
image.Save(FileName); // <<--- Out of memory exception here
// Dispose of the image
image = null;
当我尝试保存文件时,出现内存不足异常。关于如何解决这个问题的任何想法?
最佳答案
由于 WebImage
中的错误,我不得不求助于以下代码:
// Save a thumbnail of the file
byte[] fileBytes = System.IO.File.ReadAllBytes(savedFileName);
System.Drawing.Image i;
using (MemoryStream ms = new MemoryStream())
{
ms.Write(fileBytes, 0, fileBytes.Length);
i = System.Drawing.Image.FromStream(ms);
}
// Create the thumbnail
System.Drawing.Image thumbnail = i.GetThumbnailImage(135, 150, () => false, IntPtr.Zero);
关于c# - 使用 WebImage.Resize 时出现内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5271585/