簡體   English   中英

System.Drawing.Image中的內存不足異常

[英]Out of memory exception in System.Drawing.Image

我需要調整3種不同大小的文件上傳圖像的大小(每個圖像),並將它們的路徑保存到數據庫中。

我為此寫了belove方法。

public void resize(string tempPath,string fname,string extension,int x,int y,string path) {

          System.Drawing.Image img = System.Drawing.Image.FromFile(tempPath);

            using (MemoryStream memory = new MemoryStream())
            {
                Bitmap tnBitmap = new Bitmap(img);
                Graphics tnGraph = Graphics.FromImage(tnBitmap);
                tnGraph.CompositingQuality = CompositingQuality.HighQuality;
                //settings ..
                double ratioX = (double)x / (double)tnBitmap.Width;
                double ratioY = (double)y / (double)tnBitmap.Height;
                double ratio = ratioX < ratioY ? ratioX : ratioY;
                int newHeight = Convert.ToInt32(tnBitmap.Height * ratio);
                int newWidth = Convert.ToInt32(tnBitmap.Width * ratio);    
                int posX = Convert.ToInt32((x - (tnBitmap.Width * ratio)) / 2);
                int posY = Convert.ToInt32((y - (tnBitmap.Height * ratio)) / 2);
                tnGraph.DrawImage(img, posX, posY, newWidth, newHeight);    
                img.Dispose();
                using (FileStream fs = new FileStream(tempPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {                      
                    tnBitmap.Save(memory, ImageFormat.Jpeg);
                    byte[] bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                    fs.Close();
                    try
                    {
                        FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://ftp.address.com/httpdocs/img-cdn" + @"\" + fname + extension);    
                        req.UseBinary = true;
                        req.Method = WebRequestMethods.Ftp.UploadFile;
                        req.Credentials = new NetworkCredential("username", "pw");
                        StreamReader rdr = new StreamReader(memory);

                        rdr.Close();
                        req.ContentLength = memory.ToArray().Length;

                        Stream reqStream = req.GetRequestStream();

                        reqStream.Write(memory.ToArray(), 0, memory.ToArray().Length);
                        reqStream.Close();
                    }
                    catch (WebException e)
                    {
                        String status = ((FtpWebResponse)e.Response).StatusDescription;
                    }                       
                    path = "www.address.com/" + fname + extension;
                }
                memory.Dispose();
                tnBitmap.Dispose();
                tnGraph.Dispose();    
            }

}

方法調用:

public ActionResult addPathFu(string ID){
     List<ProductImage> lPi = new List<ProductImage>();
     string mTempPath = Path.GetTempFileName();
     string sTempPath = Path.GetTempFileName();
     string bTempPath = Path.GetTempFileName();
     string extension = "";
     for (int i = 0; i < Request.Files.Count; i++)
     {            
          ProductImage pi = new ProductImage();
          //bigPath file 
          var bigFileName = $@"{DateTime.Now.Ticks}-b";
          extension = Path.GetExtension(Request.Files[i].FileName);
          Request.Files[i].SaveAs(bTempPath);              
          resize(bTempPath,bigFileName,extension,1500,1500,pi.BigPath);

          //midpath 
          var midFileName = $@"{DateTime.Now.Ticks}-m";
          extension=Path.GetExtension(Request.Files[i].FileName);
          resize(mTempPath, midFileName, extension, 500, 750,pi.MidPath);

          Request.Files[i].SaveAs(mTempPath);

          //smallpath
          var sFileName = $@"{DateTime.Now.Ticks}-s";
          extension = Path.GetExtension(Request.Files[i].FileName);
          Request.Files[i].SaveAs(sTempPath);
          resize(sTempPath, sFileName, extension, 295, 443,pi.SmallPath);

          lPi.Add(pi);
     }
  }

問題是,我第二次在運行時調用此方法, System.Drawing.Image img = System.Drawing.Image.FromFile(tempPath); 語句拋出錯誤:

內存不足異常。

我丟棄了所有可丟棄的東西。 我也在web.config中啟用了:

<runtime>
  <gcAllowVeryLargeObjects enabled="true" />
</runtime>

我會使用過多的內存嗎? 我怎樣才能解決這個問題?

您的例行聲明中有太多“路徑”,perharps會縮短它,例如:

public void resize(string tempPath, string fname, string extension, int x, int y, string path, Image img)

您剛編輯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM