2014-01-25 7 views
0
private void button4_Click(object sender, EventArgs e) 
{ 
    string originalPathFile = @"C:\Users\user\Downloads\CaptchaCollection\Small\Sorting\"; 
    string newPathFile = @"C:\Users\user\Downloads\CaptchaCollection\Small\Sorted\"; 


    bool endInner = false; 
    int count2 = 1; 
    while (!endInner) 
    { 
     var files = Directory.GetFiles(originalPathFile).Select(nameWithExtension => Path.GetFileNameWithoutExtension(nameWithExtension)).Where(name => { int number; return int.TryParse(name, out number); }).Select(name => int.Parse(name)).OrderBy(number => number).ToArray(); 

     Bitmap im1 = new Bitmap(originalPathFile + files[0].ToString() + ".png"); 
     Bitmap im2 = new Bitmap(originalPathFile + files[count2].ToString() + ".png"); 

     if (compare(im1, im2)) 
     { 
      // if it's equal 
      File.Move(originalPathFile + files[count2].ToString() + ".png", newPathFile + files[count2].ToString() + ".png"); 
      MessageBox.Show(files[count2].ToString() + " was removed"); 
     } 

     if (count2 >= files.Length - 1) // checks if reached last file in directory 
     { 
      endInner = true; 
     } 

     count2++; 
    } 
} 

첫 번째 색인 (다음 이미지로 가기 위해 중첩 된 색인을 만드는 등)을 비교하여 시각적으로 복제 된 모든 이미지를 이동시키는 버튼입니다. 2 개의 경로 파일 문자열을 만듭니다. 그런 다음 while 루프를 사용하여 내 개수가 디렉토리에있는 파일의 양에 도달했는지 확인합니다. 그런 다음 루프를 종료합니다.내 이미지가 열린 것으로 간주되는 이유는 무엇입니까?

private bool compare(Bitmap bmp1, Bitmap bmp2) 
{ 
    bool equals = true; 
    Rectangle rect = new Rectangle(0, 0, bmp1.Width, bmp1.Height); 
    BitmapData bmpData1 = bmp1.LockBits(rect, ImageLockMode.ReadOnly, bmp1.PixelFormat); 
    BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadOnly, bmp2.PixelFormat); 
    unsafe 
    { 
     byte* ptr1 = (byte*)bmpData1.Scan0.ToPointer(); 
     byte* ptr2 = (byte*)bmpData2.Scan0.ToPointer(); 
     int width = rect.Width * 3; // for 24bpp pixel data 
     for (int y = 0; equals && y < rect.Height; y++) 
     { 
      for (int x = 0; x < width; x++) 
      { 
       if (*ptr1 != *ptr2) 
       { 
        equals = false; 
        break; 
       } 
       ptr1++; 
       ptr2++; 
      } 
      ptr1 += bmpData1.Stride - width; 
      ptr2 += bmpData2.Stride - width; 
     } 
    } 
    bmp1.UnlockBits(bmpData1); 
    bmp2.UnlockBits(bmpData2); 

    return equals; 
} 

이 방법은 이미지가 복제되었는지 여부를 시각적으로 확인합니다. 반환되는 경우 true을 반환합니다.

나는이 예외를 받고 있어요 :

The process cannot access the file because it is being used by another process. 

그것은이 라인에서 발생

File.Move(originalPathFile + files[count2].ToString() + ".png", newPathFile + files[count2].ToString() + ".png"); 
+0

은 비트 맵을 폐기하십시오. – Aybe

+0

어떻게 처분합니까? 나는 이것이 어떻게 행해졌는지 정말로 모른다. – puretppc

+2

bitmap.Dispose()를 호출하십시오. 더 나은 아직 'using'문을 사용하십시오. – Aybe

답변

0

을 열고 파일을 사용 중이므로이 파일을 이동할 수 없습니다 new Bitmap(fileName)를 사용하는 경우. 먼저 해당 개체를 처리하고 파일을 이동하십시오. 당신은 또한 비트 맵 대신에 파일명을 보낼 수 있고 다음과 같이 자동으로 객체를 처리 할 keywork를 사용하여 비교 함수를 사용할 수 있습니다.

compare(originalPathFile + files[0].ToString() + ".png", originalPathFile + files[count2].ToString() + ".png") 


private bool compare(String bmp1Path, String bmp2Path) 
{ 
    bool equals = true; 
    Rectangle rect = new Rectangle(0, 0, bmp1.Width, bmp1.Height); 

using(Bitmap im1 = new Bitmap(bmp1Path) 
{ 
using(Bitmap im2 = new Bitmap(bmp2Path) 
{ 

BitmapData bmpData1 = bmp1.LockBits(rect, ImageLockMode.ReadOnly, bmp1.PixelFormat); 
BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadOnly, bmp2.PixelFormat); 
unsafe 
{ 
    byte* ptr1 = (byte*)bmpData1.Scan0.ToPointer(); 
    byte* ptr2 = (byte*)bmpData2.Scan0.ToPointer(); 
    int width = rect.Width * 3; // for 24bpp pixel data 
    for (int y = 0; equals && y < rect.Height; y++) 
    { 
     for (int x = 0; x < width; x++) 
     { 
      if (*ptr1 != *ptr2) 
      { 
       equals = false; 
       break; 
      } 
      ptr1++; 
      ptr2++; 
     } 
     ptr1 += bmpData1.Stride - width; 
     ptr2 += bmpData2.Stride - width; 
    } 
} 
bmp1.UnlockBits(bmpData1); 
bmp2.UnlockBits(bmpData2); 
} 
} 
return equals; 

}