【发布时间】:2015-02-16 03:31:39
【问题描述】:
当用户向我们发送图像时,我正在运行以下代码来创建缩略图:
public int AddThumbnail(byte[] originalImage, File parentFile)
{
File tnFile = null;
try
{
System.Drawing.Image image;
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(originalImage))
{
image = System.Drawing.Image.FromStream(memoryStream);
}
Log.Write("Original image width of [" + image.Width.ToString() + "] and height of [" + image.Height.ToString() + "]");
//dimensions need to be changeable
double factor = (double)m_thumbnailWidth / (double)image.Width;
int thHeight = (int)(image.Height * factor);
byte[] tnData = null;
Log.Write("Thumbnail width of [" + m_thumbnailWidth.ToString() + "] and height of [" + thHeight + "]");
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(m_thumbnailWidth, thHeight, () => false, IntPtr.Zero))
{
using (System.IO.MemoryStream tnStream = new System.IO.MemoryStream())
{
thumbnail.Save(tnStream, System.Drawing.Imaging.ImageFormat.Jpeg);
tnData = new byte[tnStream.Length];
tnStream.Position = 0;
tnStream.Read(tnData, 0, (int)tnStream.Length);
}
}
//there is other code here that is not relevant to the problem
}
catch (Exception ex)
{
Log.Error(ex);
}
return (tnFile == null ? -1 : tnFile.Id);
}
这在我的机器上运行良好,但是当我在测试服务器上运行它时,我总是在线上出现内存不足异常: 使用 (System.Drawing.Image thumbnail = image.GetThumbnailImage(m_thumbnailWidth, thHeight, () => false, IntPtr.Zero)) 它不是在处理大图像:它试图将 480*640 的图像转换为 96*128 的缩略图。我不知道如何调查/解决这个问题。有没有人有任何建议?它总是会发生,即使在我重新启动 IIS 之后也是如此。我最初确实认为图像可能已损坏,但尺寸是正确的。 谢谢。
【问题讨论】:
-
您是否只是服务器内存不足?拿出你的性能监视器,跟踪堆大小,GC gens,整体内存消耗。
-
只是一个疯狂的猜测,也许你的机器是 64 位架构,而服务器是 32 位,这意味着你的机器上有更多可用内存
-
GDI 是 notorious 用于抛出 OOM,即使内存不是问题。 GDI 经常在几乎任何错误条件下返回“未能分配内存”,.NET 库将其转换为 OOM,即使分配因其他原因而失败。 可能这是一个合法的记忆问题,但我看到很多其他人追逐这条白鲸,结果证明是别的东西(例如,编解码器标志的错误设置)。
-
服务器是 64 位的。我很惊讶操纵一个小图像给我这个问题。我会看看内存消耗等