这个调用所涉及的代码几年来一直运行良好,它从用户获取和图像,调整大小并将其保存到磁盘.该应用程序是一个开发.Net Framework 3.5的ASP.Net应用程序.
当对Bitmap.Save的调用失败时,它会引发臭名昭着的异常:
ERROR MESSAGE: Error genérico en GDI+.
STACK TRACE:
en System.Drawing.Image.Save(String filename,ImageCodecInfo encoder,EncoderParameters encoderParams)
en System.Drawing.Image.Save(String filename,ImageFormat format)
en System.Drawing.Image.Save(String filename)
我一直在阅读很多关于该异常的帖子,我完全清楚在许多不同的情况下会引发这个异常.这是一个通用的异常,由于许多不同的原因(即,当应用程序没有写入文件夹的permisison,当代码试图将图像保存在它正在读取的同一文件中时,等等),但是没有这些情况就是我们的情况.
当我们认识到该方法失败时,我们刚刚发布了我们的应用程序的新版本,所以我们最初认为错误可能在我们的代码中(尽管该部分没有被更改,我们认为我们已经改变了其他干扰的东西,产生错误),但我们调查得越多,我们就越不了解错误发生的时间.
同样重要的是,在出现错误的同时,我们还使用最新的Windows更新更新了我们的服务器,在那里我还看到了两个可疑的:
KB2929755 http://support.microsoft.com/kb/2929755/en-us
在Windows应用程序中加载某些图像资源时内存不足
KB2957503 http://support.microsoft.com/kb/2957503/en-us
MS14-036:Windows 7,Windows Server 2008 R2,Windows Server 2008,Windows Vista和Windows Server 2003安全更新说明:2014年6月10日
这些更新服务器上的GDIplus.dll以避免一些安全问题.
我的感觉是服务器上的某些东西没有被释放,就像某些代码泄漏处理程序或内存一样,并且在某些时候服务器耗尽它们并且不能再保存Bitmap.出现错误时,从应用程序的任何部分对Bitmap.Save的任何调用都会引发该异常.重新启动IIS会解决问题直到下一次,但是,如果我们只重新启动应用程序池,则情况并非如此.
如果它可以以任何方式提供帮助,我们使用HiQPdf库(版本6.8.0.0)将一些HTML文档转换为PDF.此工具为每次转换创建一个新进程,并在完成时将其终止,并假设以适当的方式释放所有使用的资源.
是否有人遇到任何类似的问题?
有人在描述Windows更新方面有任何问题吗?
我还忘了提到我们尝试使用WPF API而不是GDI调整图像大小,但我们遇到了同样的问题,几个小时后服务器开始引发错误(虽然这次消息是ExcepcióndeHRESULT:0x88982F8A),所以我们将它还原为原始代码.
任何帮助,将不胜感激!
编辑:
实际上,使图像调整大小和保存的代码如下.所有变量都在使用块内,但是对于将图像映射到Bitmap,我假设它仍然被处理掉:
using (Image originallogo = Image.FromStream(fulogo.PostedFile.InputStream)) { using (Image resizedlogo = ImageUtil.ResizeImage(originallogo,maxWidth,maxHeight)) { ((System.Drawing.Bitmap)resizedlogo).Save(newFilePath); } }
铸造到Bitmap是否需要创建一个中间变量来强制它的处置,或者不需要?
using (Image originallogo = Image.FromStream(fulogo.PostedFile.InputStream)) { using (Image resizedlogo = ImageUtil.ResizeImage(originallogo,maxHeight)) { using (Bitmap bitmap=((System.Drawing.Bitmap)resizedlogo)) { bitmap.Save(newFilePath); } } }
谢谢,
安瑞科
编辑:
public static System.Drawing.Image ResizeImage(System.Drawing.Image originalImage,int maxWidth,int maxHeight) { int imgWidth = originalImage.Width; int imgHeight = originalImage.Height; if (originalImage.Height<maxHeight && originalImage.Width<maxWidth) return originalImage; double widhtRatio = ((double)maxWidth) / ((double)imgWidth); double heightRatio = ((double)maxHeight) / ((double)imgHeight); int targetWidth,targetHeight; if (widhtRatio < heightRatio) { targetWidth = (int)(imgWidth * widhtRatio); targetHeight = (int)(imgHeight * widhtRatio); } else { targetWidth = (int)(imgWidth * heightRatio); targetHeight = (int)(imgHeight * heightRatio); } //Not all pixel formats are supported,therefore,using the //originalImage pixel format raises an exception if the pixel format is not supported. //By not specifying it,the Bitmap constructor will use one compatible with the FromImage method. //For more info see: http://forums.asp.net/p/1195630/2066153.aspx // Image resizedImage = new Bitmap(targetWidth,targetHeight,originalImage.PixelFormat); System.Drawing.Image resizedImage = new Bitmap(targetWidth,targetHeight); using (Graphics riGraphics = Graphics.FromImage(resizedImage)) { riGraphics.CompositingQuality = CompositingQuality.HighQuality; riGraphics.SmoothingMode = SmoothingMode.HighQuality; Rectangle rectangle = new Rectangle(0,targetWidth,targetHeight); riGraphics.DrawImage(originalImage,rectangle); riGraphics.Flush(); } return resizedImage; }
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。