public void RecalculateHashes(Action <int> progress) { int count = imagesDict.Count * 2; int index = 0; foreach (Image image in imagesDict.Select(x => x.Value)) { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(image.path); image.hash = DHash.Calculate(bmp); bmp.Dispose(); ++index; progress((int)(100.0f * index / count)); } foreach (Image image in imagesDict.Select(x => x.Value)) { foreach (Image image2 in imagesDict.Select(x => x.Value)) { if (image != image2) { int dist = DHash.Distance(image.hash, image2.hash); if (dist < DHash.Margin && !duplicates.Any(x => x.image1 == image2 && x.image2 == image)) { duplicates.Add(new Duplicate { image1 = image, image2 = image2, dist = dist }); } } } ++index; progress((int)(100.0f * index / count)); } }
public void Scan(Action <int> progress) { missing.Clear(); exactDuplicates.Clear(); foreach (KeyValuePair <string, Image> kvp in imagesDict) { kvp.Value.found = false; } // scan for new files, mark existing string[] files = Directory.GetFiles(path); List <string> newFiles = new List <string>(); foreach (string file in files) { string filename = Path.GetFileName(file); string ext = Path.GetExtension(file); if (!excludedExt.Contains(ext)) { if (imagesDict.TryGetValue(filename, out Image image)) { image.found = true; } else { newFiles.Add(filename); } } } // add new files, check for duplicates if (newFiles.Count != 0) { int index = 0; foreach (string newFile in newFiles) { Image image = new Image { Filename = newFile, path = $"{path}\\{newFile}", found = true }; System.Drawing.Bitmap bmp = ImageLoader.Load(image.path); image.hash = DHash.Calculate(bmp); image.width = bmp.Width; image.height = bmp.Height; image.size = new FileInfo(image.path).Length; bmp.Dispose(); foreach (Image image2 in imagesDict.Select(x => x.Value).Where(x => x.found)) { int dist = DHash.Distance(image.hash, image2.hash); if (dist == 0 && image.size == image2.size && image.ResolutionValue == image2.ResolutionValue) { // exact duplicate, autodelete exactDuplicates.Add(image.path); image = null; break; } else if (dist < DHash.Margin && !duplicates.Any(x => x.image1 == image2 && x.image2 == image)) { duplicates.Add(new Duplicate { image1 = image, image2 = image2, dist = dist }); } } if (image != null) { imagesDict[newFile] = image; newImages.Add(image); } ++index; progress(100 * index / newFiles.Count); } } missing.AddRange(imagesDict.Select(x => x.Value).Where(x => !x.found)); }