Kasun Lee

Kasun Lee

  • NA
  • 178
  • 14.4k

Finding dupicated images

Sep 23 2019 1:29 AM
Hi!
 
I have this picture folder, which has duplicated image files. I wanna get rid of the duplicates, so to identify those images, I wrote below disfunctional, amature piece of code. I was trying to insert width, height, and size of all images into Lists, and then enumarate each image again, comparing the W, H, SZ values to the previously stored values. If two images have the same w, h, sz values, I'll know they are duplicates. Can anyone please either show me how to fix the code, OR show me a Different approch to it that's simpler than this?
 
  1. private void recheckForDuplicatesToolStripMenuItem_Click(object sender, EventArgs e)  
  2.         {  
  3.             string src = Path.GetDirectoryName(Application.ExecutablePath) + "\\Pics";  
  4.   
  5.             string[] srcfiles = Directory.GetFiles(src);  
  6.   
  7.             int n = 0;  
  8.   
  9.             List<int> imgH = new List<int>();  
  10.             List<int> imgW = new List<int>();  
  11.             List<long> imgSz = new List<long>();  
  12.   
  13.             foreach (string srcfile in srcfiles)  
  14.             {  
  15.                 try  
  16.                 {  
  17.                     Image img = Image.FromFile(srcfile);  
  18.   
  19.                     imgH.Add(img.Height);  
  20.                     imgW.Add(img.Width);  
  21.                     long len = (new FileInfo(srcfile)).Length;  
  22.                     imgSz.Add(len);  
  23.   
  24.                     img.Dispose();  
  25.   
  26.                 }  
  27.                 catch (Exception ex)  
  28.                 {  
  29.                     MessageBox.Show(ex.Message);  
  30.                 }  
  31.             }  
  32.   
  33.             int x = 0;  
  34.             foreach (string srcfile in srcfiles)  
  35.             {  
  36.                 Image img = Image.FromFile(srcfile);  
  37.   
  38.                 long len = (new FileInfo(srcfile)).Length;  
  39.   
  40.                 for (int z = 0; z < imgH.Count; z++)  
  41.                 {  
  42.                     if (imgH[z] == img.Height && imgW[z] == img.Width && imgSz[z] == len)  
  43.                     {  
  44.                         if(x == z)  
  45.                         {  
  46.                             continue;  
  47.                         }  
  48.                         x++;  
  49.   
  50.                         if (n >= 2)  
  51.                         {  
  52.                             //MessageBox.Show("Possible Duplicate: \n" + srcfile + "\n" +  
  53.                             //    img.Height + " " + img.Width + " " + len);  
  54.                             n = 0;  
  55.                         }  
  56.   
  57.                         n++;  
  58.                     }  
  59.                 }  
  60.                 x = 0;  
  61.                 img.Dispose();  
  62.             }  
  63.         } 
 THANKS!!

Answers (4)