2014-01-25 49 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

我得到這個異常:當你打開文件使用new Bitmap(fileName),因爲它是在使用中不能移動文件

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

處置您的位圖。 – Aybe

+0

你如何「處置」他們?我真的不知道這是如何完成的。 – puretppc

+2

調用bitmap.Dispose()。更好的是使用'使用'語句。 – Aybe

回答

0

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

它發生在這條線上。 因此,首先處理該對象,然後嘗試移動文件。你也可以使用下面的方法,你可以發送文件名而不是位圖,在比較函數中使用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; 

}

相關問題