ASP.NET创建thumbails服务器端 [英] ASP.NET creating thumbails server side
问题描述
这看起来desceptively易于使用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服务中使用。尝试使用这些类从这些类型的应用程序之一中可能会产生意想不到的问题,如降低服务性能和运行时异常。
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 + code的范围内intermittented内存不足的错误。我开始怀疑这是原因。
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?
相关code以下。唯一的例外间歇发生在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 +,并把使用(){}
周围的一切这就是的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.
您可以考虑使用<一个href=\"http://www.google.co.in/search?hl=en&source=hp&q=C%23%3A+HTTP+handlers+for+image+manipulation&btnG=Google+Search&meta=&aq=f&oq=\"相对=nofollow> HTTP处理程序的,
处置System.Drawing中的对象应该是一个优先级(的使用(){}语句)
Disposing System.Drawing objects should be a priority(using(){} statement )
异步页可以在这里探索。
这篇关于ASP.NET创建thumbails服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!