I am trying to implement a tiny image tagger with customized tag options in C#. The main window is as follows.
The left block is a picture box MainPictureBox
and there are two buttons, PreviousButton
and NextButton
. Furthermore, there are some tags in a group box AttributeGroupBox
. A save button SaveButton
is used for saving tagged information.
The implemented functions:
Load images automatically in the folder which the program located.
Each image could be tagged.
The tag information of each image could be saved as a text file
LabelResult.txt
.
The experimental implementation
Attribute
Class
class Attribute
{
public enum AttributeEnum
{
Mountain,
Cat
}
public AttributeEnum attributeEnum;
public override string ToString()
{
if (this.attributeEnum.Equals(AttributeEnum.Cat))
{
return "Cat";
}
if (this.attributeEnum.Equals(AttributeEnum.Mountain))
{
return "Mountain";
}
return "";
}
}
- Main Winform
public partial class Form1 : Form
{
List<string> ImagePaths;
List<Attribute> AttributeList;
int index = 0;
string targetDirectory = "./";
public Form1()
{
InitializeComponent();
System.IO.FileSystemWatcher watcher = new FileSystemWatcher()
{
Path = targetDirectory,
Filter = "*.jpg | *.jpeg| *.bmp | *.png"
};
// Add event handlers for all events you want to handle
watcher.Created += new FileSystemEventHandler(OnChanged);
// Activate the watcher
watcher.EnableRaisingEvents = true;
ImagePaths = new List<string>();
string[] fileEntries = System.IO.Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
{
string filenameExt = System.IO.Path.GetExtension(fileName);
if (filenameExt.Equals(".jpg") ||
filenameExt.Equals(".jpeg") ||
filenameExt.Equals(".bmp") ||
filenameExt.Equals(".png")
)
{
ImagePaths.Add(fileName);
}
}
MainPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
MainPictureBox.Image = Image.FromFile(ImagePaths[0]);
AttributeList = new List<Attribute>();
}
private void OnChanged(object source, FileSystemEventArgs e)
{
ImagePaths.Clear();
string[] fileEntries = System.IO.Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
{
string filenameExt = System.IO.Path.GetExtension(fileName);
if (filenameExt.Equals(".jpg") ||
filenameExt.Equals(".jpeg") ||
filenameExt.Equals(".bmp") ||
filenameExt.Equals(".png")
)
{
ImagePaths.Add(fileName);
}
}
}
private void PreviousButton_Click(object sender, EventArgs e)
{
index--;
if (index <= 0)
{
index = 0;
}
MainPictureBox.Image = Image.FromFile(ImagePaths[index]);
GC.Collect();
}
private void NextButton_Click(object sender, EventArgs e)
{
NextAction();
}
private void NextAction()
{
index++;
if (index >= ImagePaths.Count)
{
index = ImagePaths.Count - 1;
}
MainPictureBox.Image = Image.FromFile(ImagePaths[index]);
GC.Collect();
}
private void SaveButton_Click(object sender, EventArgs e)
{
Attribute attribute = new Attribute();
if (radioButton1.Checked)
{
attribute.attributeEnum = Attribute.AttributeEnum.Mountain;
}
if (radioButton2.Checked)
{
attribute.attributeEnum = Attribute.AttributeEnum.Cat;
}
MessageBox.Show(attribute.ToString());
AttributeList.Add(attribute);
AttributeListToTxt();
NextAction();
}
private void AttributeListToTxt()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.AttributeList.Count; i++)
{
sb.Append(this.ImagePaths[i] + "\t" + this.AttributeList[i].ToString() + Environment.NewLine);
}
File.WriteAllText("LabelResult.txt", sb.ToString());
}
}
All suggestions are welcome.
If there is any possible improvement about:
- Potential drawback or unnecessary overhead
- The design of implemented methods
please let me know.