2012-02-12 3 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-failureing-when-image-fromfile-before-it-made-making-copy) – BrokenGlass

+1

코드의 관련 비트 만 게시하십시오. – svick

+0

@svick - 문제는 거기에 있었고 나는 그것을 지적 할 수 없다고 생각합니다. 그래서 모든 코드를 게시하여 문제가되는 코드를 알려줄 수 있습니다 :) –

답변

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); 
[Image.FromFile가 그 이전에 호출 될 때 File.delete를가로드 된 이미지의 복사본을 만들고 원본 파괴에도 불구하고, 실패 하나]의
+0

. 이게 내가 –

+0

@ kazu.zushifukato를 찾고있는 해결책이었습니다.이 경우 [이 대답을 받아 들여야합니다] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer -work/5235 # 5235). – svick