当用户向我们发送图像时,我正在运行以下代码来创建缩略图:
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 缩略图 = image.GetThumbnailImage(m_thumbnailWidth, thHeight, () => false, IntPtr.Zero))
它不是在处理大图像:它正在尝试将 480*640 图像转换为 96*128 缩略图。我不知道如何调查/解决这个问题。有人有什么建议吗?即使我重新启动 IIS 之后,这种情况总是会发生。我最初确实认为图像可能已损坏,但尺寸是正确的。
谢谢。
我们在 ASP.Net 服务器中使用 GDI+ 操作时也遇到了类似的问题。您提到在服务器上运行的代码让我觉得这可能是同样的问题。
请注意,类的使用System.Drawing
服务器不支持命名空间。致命的是,它可能工作一段时间后突然(即使没有更改代码)出现错误。
我们必须重写服务器代码的重要部分。
看评论:
注意事项
System.Drawing 命名空间中的类是
不支持在 Windows 或 ASP.NET 服务中使用。尝试
从这些应用程序类型之一中使用这些类可能
产生意想不到的问题,例如服务性能下降
和运行时异常。有关受支持的替代方案,请参阅 Windows
成像组件。
Source:
http://msdn.microsoft.com/de-de/library/system.drawing(v=vs.110).aspx http://msdn.microsoft.com/de-de/library/system.drawing(v=vs.110).aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)