using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace AgraV2 { public partial class Form1 : Form { public DirectoryInfo selectedDirectory; public Bitmap selectedImage; public List effects; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { effects = new List(); effects.Add(new EffectGrayScale("GrayScale", pnlEffectToolbox)); effects.Add(new EffectMedianFilter("Median Filter", pnlEffectToolbox)); RefreshUi(); } private void RefreshUi() { Invalidate(); pbxOriginal.Image = selectedImage; lsbEffects.DataSource = effects; } private void btnChangeFolder_Click(object sender, EventArgs e) { const int iconWidthRatio = 1; const int iconHeightRatio = 10; FolderBrowserDialog dialog = new FolderBrowserDialog(); if (dialog.ShowDialog() == DialogResult.OK) { selectedDirectory = new DirectoryInfo(dialog.SelectedPath); List images = new List(); images.AddRange(selectedDirectory.GetFiles("*.png").ToList()); images.AddRange(selectedDirectory.GetFiles("*.jpg").ToList()); PictureBox genericPbx = new PictureBox(); //The -pnlFiles/10 is only here to account for the scrollbar genericPbx.Size = new Size(pnlFiles.Width / iconWidthRatio - pnlFiles.Width / 10, pnlFiles.Height / iconHeightRatio); genericPbx.SizeMode = PictureBoxSizeMode.Zoom; int fileCount = 0; foreach (FileInfo file in images) { string fullName = selectedDirectory.FullName + "\\" + file.Name; PictureBox currentPbx = new PictureBox(); currentPbx.Size = genericPbx.Size; currentPbx.SizeMode = genericPbx.SizeMode; currentPbx.Click += Image_Click; currentPbx.Location = new Point(0, fileCount * pnlFiles.Height / iconHeightRatio); currentPbx.Name = fullName; currentPbx.Image = Image.FromFile(fullName); pnlFiles.Controls.Add(currentPbx); fileCount++; } } else { MessageBox.Show("Ooops... Could not retrieve folder informations :("); } RefreshUi(); } private void Image_Click(object sender, EventArgs e) { if (sender is PictureBox) { PictureBox pbx = sender as PictureBox; selectedImage = (Bitmap)Image.FromFile(pbx.Name); } RefreshUi(); } private void btnApply_Click(object sender, EventArgs e) { if (lsbEffects.SelectedIndex >= 0) { Effect effect = effects[lsbEffects.SelectedIndex]; Bitmap[] results = effect.apply((Bitmap)pbxOriginal.Image); if (results != null && results.Length > 0) { if (results.Length == 3) { pbxResult1.Image = results[0]; pbxResult2.Image = results[1]; pbxResult3.Image = results[2]; if (effect.ProcessingDuration != null && effect.ProcessingDuration.Count > 0) { lblTimer1.Visible = lblTimer2.Visible = lblTimer3.Visible = true; lblTimer1.Text = effect.ProcessingDuration[0] + "s"; lblTimer2.Text = effect.ProcessingDuration[1] + "s"; lblTimer3.Text = effect.ProcessingDuration[2] + "s"; } } else { pbxResult1.Image = results[0]; if (effect.ProcessingDuration != null && effect.ProcessingDuration.Count > 0) { lblTimer1.Visible = true; lblTimer1.Text = effect.ProcessingDuration[0] + "s"; } } } } } private void DownloadImage(Image image) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "SaveImg"; if (dialog.ShowDialog() == DialogResult.OK) { string path = dialog.SelectedPath; image.Save(path + "\\" + "ResultImage.png", System.Drawing.Imaging.ImageFormat.Png); } } private void btnDowload1_Click(object sender, EventArgs e) { DownloadImage(pbxResult1.Image); } private void btnDownload2_Click(object sender, EventArgs e) { DownloadImage(pbxResult2.Image); } private void btnDownload3_Click(object sender, EventArgs e) { DownloadImage(pbxResult3.Image); } private void lsbEffects_SelectedIndexChanged(object sender, EventArgs e) { if (lsbEffects.SelectedIndex >= 0) { effects[lsbEffects.SelectedIndex].populatePannel(); } } } }