2012-02-12 66 views
0

我有一個應用程序,它會裁剪圖像並保存它
該過程是加載圖像,裁剪它,刪除原始圖像(所以我可以替換它),並保存它。
這是我的代碼:IOException未處理

private void DetectSize(object sender, EventArgs e) 
{ 
int x = 1; 
Bitmap TempImage = new Bitmap(@cwd + "\\t" + (x + 1) + ".jpg", true); 
     pictureBox.Image = (Image)TempImage.Clone(); 
     TempImage.Dispose(); 
     Bitmap imgPart = new Bitmap(pictureBox.Image); 
     int imgHeight = imgPart.Height; 
     int imgWidth = imgPart.Width; 
     HalfWidth = imgWidth/2; 
     MaxWidth = imgWidth; 
     try 
     { 
      Bitmap imgPart1 = new Bitmap(pictureBox.Image); 
      Color c; 
      for (int i = 0; i < imgPart1.Width; i++) 
      { 
       for (int j = 0; j < imgPart1.Height; j++) 
       { 
        c = imgPart1.GetPixel(i, j); 
        string cn = c.Name; 
        for (int z = 0; z <= 9; z++) 
        { 
         if (z < 10) 
         { 
          if (cn == "ff00000" + z) 
          { 
           if (i < HalfWidth) 
           { 
            MinWidth = i; 
           } 
           else 
           { 
            if (i < MaxWidth) 
            { 
             MaxWidth = i; 
            } 
           } 
          } 
         } 
         else 
         { 
          if (cn == "ff0000" + z) 
          { 
           if (i < HalfWidth) 
           { 
            MinWidth = i; 
           } 
           else 
           {  
            if (i < MaxWidth) 
            { 
             MaxWidth = i; 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
      MinWidth += 1; 
      MaxWidth -= 1; 
      MaxWidth = imgWidth - MaxWidth; 
      imgPart1.Dispose(); 
      imgPart.Dispose(); 
      lblLeftMargin.Text = Convert.ToString(MinWidth); 
      lblRightMargin.Text = Convert.ToString(MaxWidth); 

     } 
     catch (Exception ex) { MessageBox.Show(ex.Message); } 
     } 
    } 

這是用於定位將用於裁剪圖像的邊緣。

private void CropSave(object sender, EventArgs e) 
    { 
     int x = 1; 
     Bitmap croppedBitmap = new Bitmap(pictureBox.Image); 

     croppedBitmap = croppedBitmap.Clone(
     new Rectangle(
      MinWidth, 0, 
      (int)croppedBitmap.Width - MinWidth - MaxWidth, 
      1323), 
     System.Drawing.Imaging.PixelFormat.DontCare); 

     if (System.IO.File.Exists(@cwd + "\\t" + (x + 1) + ".jpg")) 
      System.IO.File.Delete(@cwd + "\\t" + (x + 1) + ".jpg"); 

     croppedBitmap.Save(@cwd + "\\t" + (x + 1) + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 

     croppedBitmap.Dispose(); 
     MessageBox.Show("File " + (x + 1) + "Done Cropping"); 
    } 

,這是圖像

錯誤的裁剪和保存顯示在行System.IO.File.Delete(@cwd + "\\t" + (x + 1) + ".jpg"

它說

該進程無法訪問該文件「C:\用戶。 ... \ t2.jpg',因爲它正在被另一個進程使用。

我想看看我在哪裏錯了幾天,仍然沒有。
請幫幫我。

+0

可能重複(http://stackoverflow.com/questions/3661799/file-delete-failing-when-image-fromfile-was-called-before-it-though-making-copy) – BrokenGlass 2012-02-12 17:13:57

+1

請僅發佈代碼的相關位。 – svick 2012-02-12 17:14:25

+0

@svick - 我想問題是在那裏,我不能指出它。所以我張貼了所有的代碼,以便你們可以告訴我哪一個會導致問題:) – 2012-02-12 17:37:10

回答

4
Bitmap TempImage = new Bitmap(@cwd + "\\t" + (x + 1) + ".jpg", true); 
    pictureBox.Image = (Image)TempImage.Clone(); 
    TempImage.Dispose(); 

Clone()方法沒有做到你希望的那樣。它仍然保持對文件的鎖定,內存映射文件對象在兩個圖像對象之間共享。處理第一個只關閉對象上的一個句柄,pictureBox.Image對象仍然打開另一個句柄。把它寫這樣的而不是:

pictureBox.Image = new Bitmap(TempImage); 
的[,File.Delete失敗時Image.FromFile之前,它被稱爲儘管使加載圖像的複製和破壞原]
+0

謝謝。這是我正在尋找的解決方案 – 2012-02-12 17:38:06

+0

@ kazu.zushifukato,在這種情況下,您應該[接受此答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer -work/5235#5235)。 – svick 2012-02-12 17:42:33