Duplicate File Locator 1.0
Loading...
Searching...
No Matches
DuplicatedImageFinder.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using static System.Net.Mime.MediaTypeNames;
7
8using System.Drawing;
9using Newtonsoft.Json;
10
11namespace ProofOfConcept
12{
13 public static class DuplicatedImageFinder
14 {
15 private static List<DuplicatedImage> _duplicatedImages = new List<DuplicatedImage>();
16
17 public static void AddHash(string hash, string pathToDuplicate)
18 {
19 if (_duplicatedImages.Any(img => img.Hash == hash))
20 {
21 // Get the DuplicatedImage object with the same hash value and add the path to the duplicate
22 _duplicatedImages.FirstOrDefault(img => img.Hash == hash).AddDuplicate(pathToDuplicate);
23 }
24 else
25 {
26 // Create a new object with the hash and path to the duplicate
27 _duplicatedImages.Add(new DuplicatedImage(hash, pathToDuplicate));
28 }
29 }
30
31 public static void SaveData(string saveDataFilePath)
32 {
33 string json = JsonConvert.SerializeObject(_duplicatedImages);
34 File.WriteAllText(saveDataFilePath, json);
35 }
36
37 public static void LoadData(string saveDataFilePath)
38 {
39 string json = File.ReadAllText(saveDataFilePath);
40 _duplicatedImages = JsonConvert.DeserializeObject<List<DuplicatedImage>>(json);
41
42 }
43
44 public static void OutputData(string outputFilePath)
45 {
46 if(File.ReadLines(outputFilePath).First() == "No duplicates found.")
47 {
48 File.WriteAllText(outputFilePath, "");
49 }
50
51 using (StreamWriter sw = File.AppendText(outputFilePath))
52 {
53 foreach (var img in _duplicatedImages)
54 {
55 sw.WriteLine(img);
56 }
57 }
58 }
59
60 public static string DisplayData()
61 {
62 string displayOutput = String.Empty;
63 foreach (var img in _duplicatedImages)
64 {
65 displayOutput += img;
66 }
67
68 if (displayOutput == String.Empty)
69 {
70 displayOutput = "No duplicates found.";
71 }
72
73 return displayOutput;
74 }
75
76 public static void FindOriginals(List<string> filePaths, List<string> hashesFound)
77 {
78 if (filePaths.Count == hashesFound.Count)
79 {
80 foreach(var img in _duplicatedImages)
81 {
82 int i = hashesFound.IndexOf(img.Hash);
83 string ogPath = filePaths[i];
84 img.OriginalPath = ogPath;
85 }
86 }
87 else
88 {
89 Console.WriteLine("Note: Can't find original files as filePaths and hashesFound are different in length");
90 }
91 }
92
93 public static void VerifyDuplicates()
94 {
95 // List of paths that are not duplicates
96 List<string> NotDuplicatePaths = new List<string>();
97
98 for (int i = 0; i < _duplicatedImages.Count; i++)
99 {
100 string ogPath = _duplicatedImages[i].OriginalPath;
101 Bitmap img1 = new Bitmap(ogPath);
102 foreach (var duplicatedPath in _duplicatedImages[i].DuplicateImages)
103 {
104 Bitmap img2 = new Bitmap(duplicatedPath);
105
106 if (!AreImagesEqual(img1 , img2))
107 {
108 NotDuplicatePaths.Add(duplicatedPath);
109 }
110 }
111
112 // If the "duplicate images" were not actually duplicates
113 // If there is just one added, then no possible duplicates
114 if (NotDuplicatePaths.Count > 1)
115 {
116 DuplicatedImage temp = new DuplicatedImage(_duplicatedImages[i].Hash);
117 temp.OriginalPath = NotDuplicatePaths[0];
118 NotDuplicatePaths.RemoveAt(0);
119 foreach (var path in NotDuplicatePaths)
120 {
121 temp.AddDuplicate(path);
122 }
123
124 _duplicatedImages.Add(temp);
125 }
126 }
127 }
128
129 private static bool AreImagesEqual(Bitmap bmp1, Bitmap bmp2)
130 {
131 if (bmp1.Width != bmp2.Width || bmp1.Height != bmp2.Height)
132 return false;
133
134 for (int y = 0; y < bmp1.Height; y++)
135 {
136 for (int x = 0; x < bmp1.Width; x++)
137 {
138 if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
139 return false;
140 }
141 }
142
143 return true;
144 }
145 }
146}