Skip to content

Commit f6d1225

Browse files
author
Kabuto_W
committed
Support multiple folders.
1 parent a4ef729 commit f6d1225

File tree

4 files changed

+126
-258
lines changed

4 files changed

+126
-258
lines changed

SimilarImages/SimilarImages/Form1.Designer.cs

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SimilarImages/SimilarImages/Form1.cs

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ namespace SimilarImages
1313
{
1414
public partial class Form1 : Form
1515
{
16-
private string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
1716
private int precision;
1817
private int threshold;
1918
private ImageHash.HashEnum hashEnum;
2019
private InterpolationMode interpolationMode;
20+
21+
private List<string> folderPathes = new List<string>();
2122
private List<Tuple<string, string, double>> tuples = null;
2223
private readonly bool isSimplifiedChinese = CultureInfo.CurrentUICulture.Name == "zh-CN";
2324

@@ -40,44 +41,44 @@ private void Form1_Load(object sender, EventArgs e)
4041
tkb_Threshold.Value = 80;
4142
cmb_Algorithm.SelectedIndex = 0;
4243
cmb_Interpolation.SelectedIndex = 0;
43-
tb_Directory.Text = folderPath;
44+
tb_Directory.Text = null;
4445
}
4546

46-
private void btn_Directory_Click(object sender, EventArgs e)
47+
private void btn_Clear_Click(object sender, EventArgs e)
4748
{
48-
string description = "Choose a folder to find similar images.";
49-
if (isSimplifiedChinese) { description = "选择一个文件夹来寻找相似的图片。"; }
50-
FolderBrowserDialog fbd = new FolderBrowserDialog
51-
{
52-
Description = description,
53-
ShowNewFolderButton = false
54-
};
55-
fbd.ShowDialog();
56-
if (string.IsNullOrEmpty(fbd.SelectedPath)) { return; }
57-
tb_Directory.Text = fbd.SelectedPath;
58-
folderPath = fbd.SelectedPath;
49+
folderPathes.Clear();
50+
tb_Directory.Text = null;
51+
ClearResult();
5952
}
6053

6154
private void Form1_DragEnter(object sender, DragEventArgs e)
6255
{
6356
if (e.Data.GetDataPresent(DataFormats.FileDrop))
6457
{
65-
var path = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
66-
if (Directory.Exists(path))
58+
string[] pathes = (string[])e.Data.GetData(DataFormats.FileDrop);
59+
foreach (string path in pathes)
6760
{
68-
e.Effect = DragDropEffects.Copy;
69-
return;
61+
if (Directory.Exists(path))
62+
{
63+
e.Effect = DragDropEffects.Copy;
64+
return;
65+
}
7066
}
7167
}
68+
7269
e.Effect = DragDropEffects.None;
70+
MessageBox.Show(
71+
isSimplifiedChinese ? "仅支持文件夹。" : "Support folder(s) only.",
72+
isSimplifiedChinese ? "提示" : "Notice",
73+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
7374
}
7475

7576
private void Form1_DragDrop(object sender, DragEventArgs e)
7677
{
7778
if (e.Data.GetDataPresent(DataFormats.FileDrop))
7879
{
79-
tb_Directory.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
80-
folderPath = tb_Directory.Text;
80+
folderPathes.AddRange((string[])e.Data.GetData(DataFormats.FileDrop));
81+
tb_Directory.Text = string.Join("; ", folderPathes);
8182
}
8283
}
8384

@@ -168,48 +169,46 @@ private void btn_Process_Click(object sender, EventArgs e)
168169
// Check config
169170
precision = tkb_Precision.Value;
170171
threshold = tkb_Threshold.Value;
171-
172-
string tip = "Directory does not exist.";
173-
if (isSimplifiedChinese)
172+
if (folderPathes.Count == 0 ||
173+
!folderPathes.TrueForAll((path) => Directory.Exists(path)))
174174
{
175-
tip = "文件夹不存在。";
175+
MessageBox.Show(
176+
isSimplifiedChinese ? "文件夹不存在。" : "Directory does not exist.",
177+
isSimplifiedChinese ? "提示" : "Notice",
178+
MessageBoxButtons.OK, MessageBoxIcon.Information);
179+
return;
176180
}
177-
if (!AssertConfig(Directory.Exists(tb_Directory.Text), tip)) { return; }
178181

179182
// Dispose previous items
180-
pictureBox1.Image?.Dispose();
181-
pictureBox1.Image = null;
182-
lb_Image1.Text = isSimplifiedChinese ? "图片 1" : "Image 1";
183-
lb_Image1.Tag = null;
184-
lb_Resolution1.Text = "";
185-
pictureBox2.Image?.Dispose();
186-
pictureBox2.Image = null;
187-
lb_Image2.Text = isSimplifiedChinese ? "图片 2" : "Image 2";
188-
lb_Image2.Tag = null;
189-
lb_Resolution2.Text = "";
183+
ClearResult();
190184

191185
// Process
192186
progressBar1.Visible = true;
193187
btn_Process.Enabled = false;
194188
bgw_Calculate.RunWorkerAsync();
195189
}
196190

197-
private bool AssertConfig(bool successCondition, string failureTip)
191+
private void ClearResult()
198192
{
199-
if (!successCondition)
200-
{
201-
MessageBox.Show(failureTip, isSimplifiedChinese ? "提示" : "Notice",
202-
MessageBoxButtons.OK, MessageBoxIcon.Information);
203-
}
204-
return successCondition;
193+
lvw_Result.Items.Clear();
194+
pictureBox1.Image?.Dispose();
195+
pictureBox1.Image = null;
196+
lb_Image1.Text = isSimplifiedChinese ? "图片 1" : "Image 1";
197+
lb_Image1.Tag = null;
198+
lb_Resolution1.Text = null;
199+
pictureBox2.Image?.Dispose();
200+
pictureBox2.Image = null;
201+
lb_Image2.Text = isSimplifiedChinese ? "图片 2" : "Image 2";
202+
lb_Image2.Tag = null;
203+
lb_Resolution2.Text = null;
205204
}
206205

207206
private void bgw_Calculate_DoWork(object sender, DoWorkEventArgs e)
208207
{
209208
Stopwatch watch = new Stopwatch();
210209
watch.Start();
211210

212-
tuples = ImageHash.GetSimilarity(folderPath, out int count,
211+
tuples = ImageHash.GetSimilarity(folderPathes, out int count,
213212
precision, interpolationMode, hashEnum, threshold);
214213
lb_Count.Invoke((Action)(() => { lb_Count.Text = count.ToString(); }));
215214

@@ -260,31 +259,31 @@ private void lvw_Result_SelectedIndexChanged(object sender, EventArgs e)
260259
var selectedTuple = tuples[lvw_Result.SelectedIndices[0]];
261260
try
262261
{
263-
pictureBox1.Image = new Bitmap(Path.Combine(folderPath, selectedTuple.Item1));
264-
lb_Image1.Text = selectedTuple.Item1;
265-
lb_Image1.Tag = Path.Combine(folderPath, selectedTuple.Item1);
262+
pictureBox1.Image = new Bitmap(selectedTuple.Item1);
263+
lb_Image1.Text = selectedTuple.Item1.Substring(selectedTuple.Item1.LastIndexOf('\\') + 1);
264+
lb_Image1.Tag = selectedTuple.Item1;
266265
lb_Resolution1.Text = $"{pictureBox1.Image.Width}*{pictureBox1.Image.Height}";
267266
}
268267
catch (ArgumentException)
269268
{
270269
pictureBox1.Image = null;
271270
lb_Image1.Text = "Deleted";
272271
lb_Image1.Tag = null;
273-
lb_Resolution1.Text = "";
272+
lb_Resolution1.Text = null;
274273
}
275274
try
276275
{
277-
pictureBox2.Image = new Bitmap(Path.Combine(folderPath, selectedTuple.Item2));
278-
lb_Image2.Text = selectedTuple.Item2;
279-
lb_Image2.Tag = Path.Combine(folderPath, selectedTuple.Item2);
276+
pictureBox2.Image = new Bitmap(selectedTuple.Item2);
277+
lb_Image2.Text = selectedTuple.Item2.Substring(selectedTuple.Item2.LastIndexOf('\\') + 1);
278+
lb_Image2.Tag = selectedTuple.Item2;
280279
lb_Resolution2.Text = $"{pictureBox2.Image.Width}*{pictureBox2.Image.Height}";
281280
}
282281
catch (ArgumentException)
283282
{
284283
pictureBox2.Image = null;
285284
lb_Image2.Text = "Deleted";
286285
lb_Image2.Tag = null;
287-
lb_Resolution2.Text = "";
286+
lb_Resolution2.Text = null;
288287
}
289288

290289
if (pictureBox1.Image.Width * pictureBox1.Image.Height >=
@@ -320,7 +319,7 @@ private void DeleteImage(PictureBox pictureBox, Label label)
320319
{
321320
pictureBox.Image.Dispose();
322321
pictureBox.Image = null;
323-
FileSystem.DeleteFile(Path.Combine(folderPath, label.Text),
322+
FileSystem.DeleteFile(label.Tag.ToString(),
324323
UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
325324
label.Text = "Deleted";
326325
}
@@ -329,13 +328,13 @@ private void DeleteImage(PictureBox pictureBox, Label label)
329328
private void btn_Open1_Click(object sender, EventArgs e)
330329
{
331330
if (pictureBox1.Image == null) { return; }
332-
Process.Start(Path.Combine(folderPath, lb_Image1.Text));
331+
Process.Start(lb_Image1.Tag.ToString());
333332
}
334333

335334
private void btn_Open2_Click(object sender, EventArgs e)
336335
{
337336
if (pictureBox2.Image == null) { return; }
338-
Process.Start(Path.Combine(folderPath, lb_Image2.Text));
337+
Process.Start(lb_Image2.Tag.ToString());
339338
}
340339

341340
private void lb_Image1_MouseHover(object sender, EventArgs e)

0 commit comments

Comments
 (0)