我們在運行時使用.net dll(http://imageresizing.net/download)進行imageresizing。它完美的作品。然而,過了一段時間(1-7天之間)發生,系統開始引發異常的甚至觀衆:OutOfMemoryException in Image Resizing
異常信息: 異常類型: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 &高度= 131
期待。 Farrukh
有多少RAM?內存碎片可以減少大量可用塊,即使沒有發生內存泄漏。避免這種情況的最佳方法是確保您有足夠的RAM來支持您的方案。另外,您是否使用DiskCache插件?如果不是,那就是你的問題。 –
感謝您的回覆。服務器上的RAM是:16GB,是的,我們正在使用DiskCache插件和imageresizer。 –
您在這裏問2個不同的問題 - 1)我的代碼出了什麼問題,2)爲什麼我在ImageResizer中遇到問題?你還在使用你的舊代碼嗎?它確實有很多內存泄漏。 –