2013-02-05 148 views
1

當我嘗試使用System.IO.File.Delete(....)刪除圖像時,我得到錯誤異常。C#圖像裁剪大小刪除

IOException was unhandled by user code

The process cannot access the file 'xxx\image\6132_15658422-b0a1-45a9-b0f9-7e9af783ad53_Temp.jpg' because it is being used by another process.

任何人都可以請給我建議?



我的主要功能裁剪,調整大小和C#中刪除圖像。

int X1 = 70, Y1 = 20, X2 = 201, Y2 = 236, w = 800, h = 600; 
    string filelocation = "image/6132_15658422-b0a1-45a9-b0f9-7e9af783ad53_Temp.jpg"; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      using (System.Drawing.Image _Image = cropImage(
               ResizeImage(
                 System.Drawing.Image.FromFile(Server.MapPath("~") +"/"+ filelocation), w, h), 
                 (new System.Drawing.Rectangle(X1, Y1, X2, Y2)))) 
      { 
       _Image.Save(Server.MapPath("~") + "/" + "image/output.jpg"); 

      } 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally { 
      File.Delete(Server.MapPath("~") + "/" + filelocation); 
     } 
    } 

對於裁切圖像功能,

public System.Drawing.Image cropImage(System.Drawing.Image image, Rectangle cropArea) 
    { 
     try 
     { 
      Bitmap bmpImage = new Bitmap(image); 
      Bitmap bmpCrop = bmpImage.Clone(cropArea, 
      bmpImage.PixelFormat); 
      return (System.Drawing.Image)(bmpCrop); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

對於調整圖像大小功能,

public System.Drawing.Image ResizeImage(System.Drawing.Image image, int maxWidth, int maxHeight) 
    { 
     try 
     { 
      var ratioX = (double)maxWidth/image.Width; 
      var ratioY = (double)maxHeight/image.Height; 
      var ratio = Math.Min(ratioX, ratioY); 

      var newWidth = (int)(image.Width * ratio); 
      var newHeight = (int)(image.Height * ratio); 

      var newImage = new Bitmap(newWidth, newHeight); 
      Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight); 

      return newImage; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

    } 

[更新]

最後我改正我的問題,因爲@landenedge建議,

  using (var mainImage = System.Drawing.Image.FromFile(Server.MapPath("~") +"/"+ filelocation)){ 
       using (System.Drawing.Image _Image = cropImage(
                ResizeImage(mainImage, w, h), 
                  (new System.Drawing.Rectangle(X1, Y1, X2, Y2)))) 
       { 
        _Image.Save(Server.MapPath("~") + "/" + "image/output.jpg"); 

       } 
      } 
+1

提示:「它正在被另一個進程使用」... –

+0

是的,但我不知道哪個進程已被使用... –

+0

可能是您自己的:運行ProcessMonitor ...它是免費的 –

回答

1

您需要處理您使用FromFile()創建的Image。嘗試是這樣的:

using (var mainImage = Image.FromFile(Server.MapPath("~") +"/"+ filelocation), w, h)) 
using (var _Image = cropImage(ResizeImage(mainImage, new Rectangle(X1, Y1, X2, Y2)))) 
{ 
    _Image.Save(Server.MapPath("~") + "/" + "image/output.jpg"); 
} 

另外,不要用throw ex;重新拋出異常 - 這樣重置堆棧跟蹤,可撤銷有價值的調試信息。相反,只需使用throw;

+0

@landenedge,最後你糾正我的問題。 –

+0

非常感謝@landenedge。 –

2

即使您將所有Dispose()聲明進行匹配後,您仍會發現仍有問題。在任何大量網站中,Image.FromFile都有問題。解決方法是使用Image.FromStream

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) 
    { 
    using (Image original = Image.FromStream(fs)) 
    { 
     ... 

使用一個明確的Dispose(),一個using()陳述或將該值設置爲null,直到垃圾收集發生不能解決問題。強制垃圾收集通常是一個糟糕的主意。

+0

關於Image.FromFile與Image.FromStream的明顯建議,感謝@Ian Mercer –