我们使用 .net dll (http://imageresizing.net/download) 在运行时调整图像大小。它工作完美。但是,在一段时间(1-7 天之间)后,系统开始在偶数查看器中引发异常:
异常信息: 异常类型:OutOfMemoryException 异常消息:内存不足,无法继续执行程序。
在该异常之后,网站通常会停止工作并抛出“System.OutOfMemoryException”错误。
如果我们“回收”网站运行的应用程序池,问题就会消失,网站会立即恢复正常,而无需更改任何代码。
在 imagereiszing dll 之前,我们使用的是自定义代码,同样的问题也会发生。以下是代码。
private Bitmap ConvertImage(Bitmap input, int width, int height, bool arc)
{
if (input.PixelFormat == PixelFormat.Format1bppIndexed ||
input.PixelFormat == PixelFormat.Format4bppIndexed ||
input.PixelFormat == PixelFormat.Format8bppIndexed)
{
Bitmap unpackedBitmap = new Bitmap(input.Width, input.Height);
Graphics g = Graphics.FromImage(unpackedBitmap);
g.Clear(Color.White);
g.DrawImage(input, new Rectangle(0,0,input.Width, input.Height));
g.Dispose();
input = unpackedBitmap;
}
double aspectRatio = (double)input.Height / (double)input.Width;
int actualHeight = CommonMethods.GetIntValue(Math.Round(aspectRatio * width, 0));
Bitmap _imgOut;
if (actualHeight > height)
{
ResizeImage resizeImage = new ResizeImage(width, actualHeight, InterpolationMethod.Bicubic);
Bitmap _tempBitmap = resizeImage.Apply(input);
Bitmap _croppedBitmap = new Bitmap(width, height);
Graphics _crop = Graphics.FromImage(_croppedBitmap);
_crop.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
_crop.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
_crop.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
_crop.DrawImageUnscaledAndClipped(_tempBitmap, new Rectangle(0, 0, width, height));
_crop.Dispose();
_imgOut = _croppedBitmap;
}
else
{
ResizeImage resizeImage = new ResizeImage(width, height, InterpolationMethod.Bicubic);
_imgOut = resizeImage.Apply(input);
}
// Draw the arc if it has been requested
if (arc)
{
Graphics _arc = Graphics.FromImage(_imgOut);
_arc.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
_arc.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
_arc.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
_arc.DrawArc(new Pen(Color.White, 24), new Rectangle(-13, -13, 50, 50), 180, 90);
_arc.Dispose();
}
// job done
return _imgOut;
}
我们正在调整图像大小,例如:www.mysite.com/images/myimage.jpg?width=196&height=131
期待。 法鲁克
最佳答案
当您遇到 OutOfMemoryException(无论发生在何处)时,它可能是由应用程序中任何位置的内存泄漏引起的。使用 WinDbg 调试了数十个此类实例后,我从未发现任何最终是由于 http://imageresizing.net 中的错误而导致的。 。
也就是说,有一个简单的方法可以确定这是否是 http://imageresizing.net 的问题。或不;在 IIS 中为图像和图像大小调整创建单独的应用程序池和子文件夹应用程序。除了图像缩放器之外,不安装任何东西。下次遇到该错误时,请登录服务器并 find out which w3wp.exe instance is responsible for the massive memory usage 。
如果它在 ImageResizer 应用程序池中,请从 http://imageresizing.net/support 领取 20-50 美元的错误赏金。如果没有,您需要找出主应用程序中泄漏内容的位置。
如果您在应用程序中的其他任何地方使用 System.Drawing,那么这是第一个要查看的地方。 Check your code against this list of pitfalls 。
如果您确定要在 using 或 finally 子句中处理每个 System.Drawing.* 实例,请阅读 this excellent article a few times为了确保您没有在任何基础知识上失败,请深入研究 DebugDiag 和/或 WinDBG(请参阅 bottom of article )。
关于image - 图像大小调整中出现 OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10531984/