Skip to content

Commit

Permalink
closes #5; Show similar image count and optimize some code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kabuto_W committed Nov 18, 2021
1 parent 4360819 commit ede5db9
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 485 deletions.
84 changes: 33 additions & 51 deletions SimilarImages/SimilarImages/FormComparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,54 @@ namespace SimilarImages
{
public partial class FormMain
{
private string[] currentImagePathPair = new string[2];

private void lvw_Result_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvw_Result.SelectedItems.Count < 1 ||
lvw_Result.SelectedItems[0].Text == Message.NoResult)
{ return; }

// Dispose previous images
pictureBox1.Image?.Dispose();
pictureBox2.Image?.Dispose();
pic_Image1.Image?.Dispose();
pic_Image2.Image?.Dispose();

// Show images
var selectedTuple = tuples[lvw_Result.SelectedIndices[0]];
var selectedTuple = similarityTuples[lvw_Result.SelectedIndices[0]];
if (File.Exists(selectedTuple.Item1))
{
pictureBox1.Image = new Bitmap(selectedTuple.Item1);
pic_Image1.Image = new Bitmap(selectedTuple.Item1);
lb_Image1.Text = selectedTuple.Item1.Substring(selectedTuple.Item1.LastIndexOf('\\') + 1);
lb_Image1.Tag = selectedTuple.Item1;
lb_Resolution1.Text = $"{pictureBox1.Image.Width}*{pictureBox1.Image.Height}";
currentImagePathPair[0] = selectedTuple.Item1;
lb_Resolution1.Text = $"{pic_Image1.Image.Width}*{pic_Image1.Image.Height}";
}
else
{
pictureBox1.Image = null;
pic_Image1.Image = null;
lb_Image1.Text = Message.Deleted;
lb_Image1.Tag = null;
currentImagePathPair[0] = null;
lb_Resolution1.Text = null;
}

if (File.Exists(selectedTuple.Item2))
{
pictureBox2.Image = new Bitmap(selectedTuple.Item2);
pic_Image2.Image = new Bitmap(selectedTuple.Item2);
lb_Image2.Text = selectedTuple.Item2.Substring(selectedTuple.Item2.LastIndexOf('\\') + 1);
lb_Image2.Tag = selectedTuple.Item2;
lb_Resolution2.Text = $"{pictureBox2.Image.Width}*{pictureBox2.Image.Height}";
currentImagePathPair[1] = selectedTuple.Item2;
lb_Resolution2.Text = $"{pic_Image2.Image.Width}*{pic_Image2.Image.Height}";
}
else
{
pictureBox2.Image = null;
pic_Image2.Image = null;
lb_Image2.Text = Message.Deleted;
lb_Image2.Tag = null;
currentImagePathPair[1] = null;
lb_Resolution2.Text = null;
}

// Compare resolution
if (pictureBox1.Image != null && pictureBox2.Image != null &&
pictureBox1.Image.Width * pictureBox1.Image.Height >=
pictureBox2.Image.Width * pictureBox2.Image.Height)
if (pic_Image1.Image != null && pic_Image2.Image != null &&
pic_Image1.Image.Width * pic_Image1.Image.Height >=
pic_Image2.Image.Width * pic_Image2.Image.Height)
{
lb_Resolution1.ForeColor = Color.Green;
lb_Resolution2.ForeColor = Color.Black;
Expand All @@ -68,53 +70,53 @@ private void lvw_Result_SelectedIndexChanged(object sender, EventArgs e)

private void lb_Image1_MouseHover(object sender, EventArgs e)
{
if (lb_Image1.Tag != null)
if (currentImagePathPair[0] != null)
{
toolTipMisc.SetToolTip((Control)sender, lb_Image1.Tag.ToString());
tip_Misc.SetToolTip((Control)sender, currentImagePathPair[0]);
}
}

private void lb_Image2_MouseHover(object sender, EventArgs e)
{
if (lb_Image2.Tag != null)
if (currentImagePathPair[1] != null)
{
toolTipMisc.SetToolTip((Control)sender, lb_Image2.Tag.ToString());
tip_Misc.SetToolTip((Control)sender, currentImagePathPair[1]);
}
}

private void btn_Open1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
if (pic_Image1.Image != null && currentImagePathPair[0] != null)
{
Process.Start(lb_Image1.Tag.ToString());
Process.Start(currentImagePathPair[0]);
}
}

private void btn_Open2_Click(object sender, EventArgs e)
{
if (pictureBox2.Image != null)
if (pic_Image2.Image != null && currentImagePathPair[1] != null)
{
Process.Start(lb_Image2.Tag.ToString());
Process.Start(currentImagePathPair[1]);
}
}

private void btn_Delete1_MouseDown(object sender, MouseEventArgs e)
{
if (DeleteImage(pictureBox1, lb_Image1, e.Button == MouseButtons.Right))
if (DeleteImage(pic_Image1, lb_Image1, currentImagePathPair[0], e.Button == MouseButtons.Right))
{
UpdateSelectedIndex();
}
}

private void btn_Delete2_MouseDown(object sender, MouseEventArgs e)
{
if (DeleteImage(pictureBox2, lb_Image2, e.Button == MouseButtons.Right))
if (DeleteImage(pic_Image2, lb_Image2, currentImagePathPair[1], e.Button == MouseButtons.Right))
{
UpdateSelectedIndex();
}
}

private bool DeleteImage(PictureBox pictureBox, Label label, bool force)
private bool DeleteImage(PictureBox pictureBox, Label label, string imagePath, bool force)
{
if (pictureBox.Image == null) { return false; }

Expand All @@ -131,8 +133,7 @@ private bool DeleteImage(PictureBox pictureBox, Label label, bool force)
{
pictureBox.Image.Dispose();
pictureBox.Image = null;
FileSystem.DeleteFile(label.Tag.ToString(),
UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
FileSystem.DeleteFile(imagePath, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
label.Text = Message.Deleted;
return true;
}
Expand All @@ -142,31 +143,12 @@ private bool DeleteImage(PictureBox pictureBox, Label label, bool force)
private void UpdateSelectedIndex()
{
// Unselect current
int currentSelectedIndex = lvw_Result.SelectedIndices[0];
lvw_Result.Items[currentSelectedIndex].Selected = false;

int currentIndex = lvw_Result.SelectedIndices[0];
lvw_Result.Items[currentIndex].Selected = false;
// Select new
if (currentSelectedIndex < lvw_Result.Items.Count - 1)
{
lvw_Result.Items[currentSelectedIndex + 1].Selected = true;
}
else
{
lvw_Result.Items[0].Selected = true;
}

lvw_Result.Items[currentIndex < lvw_Result.Items.Count - 1 ? currentIndex + 1 : 0].Selected = true;
// Scroll to new
lvw_Result.EnsureVisible(lvw_Result.SelectedIndices[0]);
}

private void btn_Delete1_MouseHover(object sender, EventArgs e)
{
toolTipMisc.SetToolTip((Control)sender, Message.DeleteHelp);
}

private void btn_Delete2_MouseHover(object sender, EventArgs e)
{
toolTipMisc.SetToolTip((Control)sender, Message.DeleteHelp);
}
}
}
Loading

0 comments on commit ede5db9

Please sign in to comment.