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)); } }
private void dataGridView1_SelectionChanged(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count == 0) { return; } int index = dataGridView1.SelectedRows[0].Index, pos = 0; DuplicateItem item = items[index]; foreach (DuplicateItem item2 in items) { if (item == item2) { item2.Similarity = "---"; } else { int dist = DHash.Distance(item.image.hash, item2.image.hash); item2.Similarity = $"{DHash.ToSimilarity(dist)}%"; } if (!inInit) { bindingList.ResetItem(pos); ++pos; } } if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); } try { pictureBox1.Image = ImageLoader.Load(item.image.path); } catch (Exception) { pictureBox1.Image = null; } }
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)); }