ASP.NET创建缩略图服务器端 [英] ASP.NET creating thumbnails server side
问题描述
使用System.Drawing在ASP.NET应用程序中创建缩略图看起来非常容易.但是 MSDN会告诉您:
It look desceptively easy to use System.Drawing to create thumbnails in your ASP.NET application. But MSDN tells you:
不支持在Windows或ASP.NET服务中使用System.Drawing命名空间中的类.尝试从这些应用程序类型之一中使用这些类可能会产生意外问题,例如服务性能下降和运行时异常.
Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.
我在这种GDI +代码中看到间歇性的内存不足"错误.我开始怀疑这是原因.
I'm seeing intermittented 'out of memory' errors within this type of GDI+ code. I'm beginning to suspect this is the cause.
人们如何进行服务器端图像处理?任何人都可以推荐不会破坏我的服务器的任何替代方法吗?
How ARE people doing server side image manipulation? Can anyone recommend any alternative that WON'T blow up my server?
下面的相关代码.该异常间歇性地发生在System.Drawing.Graphics.DrawImage中.我刚刚继承了这个项目,所以我需要检查日志以查看它被击中的频率/我们获得异常的频率...
The relevant code below. The exception intermittently happens in System.Drawing.Graphics.DrawImage. I've just inherited this project, so I'd need to check the logs to see how often this is being hit / how often we get an exception...
public byte[] Resize(int newWidth, int newHeight, Image orignalImage)
{
Bitmap bitmap = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Rectangle r = new Rectangle(0, 0, newWidth, newHeight);
g.DrawImage(orignalImage, r, r.X, r.Y, orignalImage.Width, orignalImage.Height, GraphicsUnit.Pixel);
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Jpeg);
// clean up memory leaks
if (bitmap != null)
{
bitmap.Dispose();
bitmap = null;
}
if (g != null)
{
g.Dispose();
g = null;
}
return stream.ToArray();
}
更新:我已经在整个项目中搜索了我们正在使用GDI +的任何位置,并将using() { }
放在所有IDisposable
周围.自从我这样做以来,我再也没有见过一个内存不足"异常.
UPDATE: I've searched thru the whole project for anywhere we are using GDI+ and put using() { }
around everything that's IDisposable
. I haven't seen one 'out of memory' exception since I did this.
推荐答案
假设您将对每个请求执行操作",则问题可能是
Assuming you will be doing "stuff" per request, the issues might be
-
处理器密集型操作:图像处理,这可能需要一些时间.
Processor intensive operation: manipulation of images, which could take time.
如果要保存文件,将导致磁盘问题.
In case you are saving the file, it will lead to disk issues.
您可以考虑使用处理System.Drawing对象应该是优先事项(
Disposing System.Drawing objects should be a priority(using(){} statement )
异步页面可以在此处进行探索. /p>
Asynchronous Pages can be explored here.
这篇关于ASP.NET创建缩略图服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!