2011-09-12 61 views
1
private void GenerateThumbnails(double scaleFactor, string sourcePath, 
string targetPath) { 
      int wi = Convert.ToInt32(Request.QueryString["dim2"]); 
      int hi = Convert.ToInt32(Request.QueryString["dim1"]); 
      using (var image = 
System.Drawing.Image.FromFile(sourcePath)) 
      { 
       var newWidth = (int)(wi);//(image.Width * 
scaleFactor); 
       var newHeight = (int)(hi);// (image.Height * 
scaleFactor); 
       var thumbnailImg = new Bitmap(newWidth, newHeight); 
       var thumbGraph = Graphics.FromImage(thumbnailImg); 
       thumbGraph.CompositingQuality = 
CompositingQuality.HighQuality; 
       thumbGraph.SmoothingMode = SmoothingMode.HighQuality; 
       thumbGraph.InterpolationMode = 
InterpolationMode.HighQualityBicubic; 
       var imageRectangle = new Rectangle(0, 0, newWidth, 
newHeight); 
       thumbGraph.DrawImage(image, imageRectangle); 
       int getwal = newWidth - 108; 
       int gethi = newHeight - 30; 
       SolidBrush brush = new SolidBrush(Color.FromArgb(113, 
255, 255, 255)); 
       thumbGraph.DrawString("myfile", new Font("Arial", 12, 
System.Drawing.FontStyle.Bold), brush, getwal,gethi); 
       thumbnailImg.Save(targetPath, image.RawFormat); 
      } } 

喜上载图片ASP.NET的内存不足的问题

我得到的

错误内存不足。我收到错误。说明: 执行当前Web请求期间发生未处理的异常。请查看堆栈 跟踪以了解有关错误的更多信息以及源自 代码的位置。

Exception Details: System.OutOfMemoryException: Out of memory. 

Source Error: 


Line 177:   thumbGraph.InterpolationMode = 
InterpolationMode.HighQualityBicubic; Line 178:   var 
imageRectangle = new Rectangle(0, 0, newWidth, newHeight); Line 179: 
thumbGraph.DrawImage(image, imageRectangle); Line 180:   
thumbnailImg.Save(targetPath, image.RawFormat); 
+1

GenerateThumbnails()与'上传图片'有什么关系? –

+1

等一下,这是一个asp.net应用程序,您正在从Image.FromFile文件加载图像?您是否知道您正在从服务器的文件系统读取文件,而不是客户端?此外,你是否知道,为了从服务器读取它,你可能需要做Server.MapPath?这可能是也可能不是错误的原因。 – Icarus

+2

好消息是,Image类对于任何事都抱怨OutOfMemory,所以它可能是别的。像错误的路径或大小计算出错。使用调试器。 –

回答

0

尝试从内置调试器或IISExpress切换到全面的本地IIS进行测试。通常情况下,存在一些限制(特别是对于IIS Express),这些限制不会以与其他环境相同的方式公开自己。

如果这不能立即解决您的问题,最好的方法是使用VS 2010内置的内存分析工具,或下载并使用RedGate ANTS Profiler的试用版。