using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace _DMatcher { public partial class Form1 : Form { private List lFiles; private int lIndex = 0; private List rFiles; private int rIndex = 0; private int matchedCount = 0; private int umlCount = 0; private int umrCount = 0; public Form1() { InitializeComponent(); } private void lBox_TextChanged(object sender, EventArgs e) { if (Directory.Exists(lBox.Text)) { DirectoryInfo lDir = new DirectoryInfo(lBox.Text); List allLeftFiles = new List(lDir.GetFiles("*.jpg")); lFiles = new List(); foreach (FileInfo lFile in allLeftFiles) { if (lFile.Name.Contains("MatchL")) { string matchPattern = @"MatchL(?\d\d\d\d)"; Match match = Regex.Match(lFile.Name, matchPattern); int fileNumber = int.Parse(match.Groups["filenum"].Value); if(fileNumber >= matchedCount) { matchedCount = fileNumber + 1; } } else if (lFile.Name.Contains("UnmatchedL")) { string matchPattern = @"UnmatchedL(?\d\d\d\d\d)"; Match match = Regex.Match(lFile.Name, matchPattern); int fileNumber = int.Parse(match.Groups["filenum"].Value); if (fileNumber >= umlCount) { umlCount = fileNumber + 1; } } else { lFiles.Add(lFile); } } lFiles.Sort(delegate(FileInfo a, FileInfo b) { return a.CreationTime.CompareTo(b.CreationTime); }); lIndex = 0; UpdateLImage(); } } private void UpdateLImage() { ShowPanelImage(lIndex, lFiles, lPanel); ShowPanelImage(lIndex-1, lFiles, lPrevPanel); ShowPanelImage(lIndex+1, lFiles, lNextPanel); } private void lPrevButton_Click(object sender, EventArgs e) { lIndex--; if (lIndex < 0) { lIndex += lFiles.Count; } UpdateLImage(); } private void lNextButton_Click(object sender, EventArgs e) { lIndex++; if (lIndex >= lFiles.Count) { lIndex = 0; } UpdateLImage(); } private void rBox_TextChanged(object sender, EventArgs e) { if (Directory.Exists(rBox.Text)) { DirectoryInfo rDir = new DirectoryInfo(rBox.Text); List allRightFiles = new List(rDir.GetFiles("*.jpg")); rFiles = new List(); foreach (FileInfo rFile in allRightFiles) { if (rFile.Name.Contains("MatchR")) { //Don't recalculate the matched count here. I'm assuming it's calculated (and correct) //with the loading of the left directory. } else if (rFile.Name.Contains("UnmatchedR")) { string matchPattern = @"UnmatchedR(?\d\d\d\d\d)"; Match match = Regex.Match(rFile.Name, matchPattern); int fileNumber = int.Parse(match.Groups["filenum"].Value); if (fileNumber >= umrCount) { umrCount = fileNumber + 1; } } else { rFiles.Add(rFile); } } rFiles.Sort(delegate(FileInfo a, FileInfo b) { return a.CreationTime.CompareTo(b.CreationTime); }); rIndex = 0; UpdateRImage(); } } private void UpdateRImage() { ShowPanelImage(rIndex, rFiles, rPanel); ShowPanelImage(rIndex - 1, rFiles, rPrevPanel); ShowPanelImage(rIndex + 1, rFiles, rNextPanel); } private void rPrevButton_Click(object sender, EventArgs e) { rIndex--; if (rIndex < 0) { rIndex += rFiles.Count; } UpdateRImage(); } private void rNextButton_Click(object sender, EventArgs e) { rIndex++; if (rIndex >= rFiles.Count) { rIndex = 0; } UpdateRImage(); } private void ShowPanelImage(int index, List files, Panel panel) { if (index >= 0 && index < files.Count) { FileInfo file = files[index]; Bitmap bmp = (Bitmap)Image.FromFile(file.FullName); Bitmap cloneBmp = new Bitmap(panel.Width, panel.Height); BitmapImages.PasteImageToFit(bmp, cloneBmp); panel.BackgroundImage = cloneBmp; panel.BackgroundImageLayout = ImageLayout.Zoom; bmp.Dispose(); } else { panel.BackgroundImage = null; panel.BackColor = Color.Red; } } private void matchButton_Click(object sender, EventArgs e) { FileInfo lFile = lFiles[lIndex]; FileInfo rFile = rFiles[rIndex]; lFile.MoveTo(Path.Combine(lFile.DirectoryName, string.Format("MatchL{0:0000}{1}", matchedCount, lFile.Extension))); rFile.MoveTo(Path.Combine(rFile.DirectoryName, string.Format("MatchR{0:0000}{1}", matchedCount, lFile.Extension))); lFiles.Remove(lFile); rFiles.Remove(rFile); lIndex = 0; UpdateLImage(); rIndex = 0; UpdateRImage(); matchedCount++; } private void rUnmatchedButton_Click(object sender, EventArgs e) { FileInfo rFile = rFiles[rIndex]; rFile.MoveTo(Path.Combine(rFile.DirectoryName, "UnmatchedR" + umrCount.ToString("00000") + rFile.Extension)); rFiles.Remove(rFile); rIndex = 0; UpdateRImage(); umrCount++; } private void lUnmatchedButton_Click(object sender, EventArgs e) { FileInfo lFile = lFiles[lIndex]; lFile.MoveTo(Path.Combine(lFile.DirectoryName, "UnmatchedL" + umlCount.ToString("00000") + lFile.Extension)); lFiles.Remove(lFile); lIndex = 0; UpdateLImage(); umlCount++; } } public static class BitmapImages { public static void PasteImageToFit(Bitmap src, Bitmap dest) { Graphics destDraw = Graphics.FromImage(dest); destDraw.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; destDraw.Clear(Color.Black); PasteImageToFit(src, destDraw, dest.Size); destDraw.Dispose(); } public static void PasteImageToFit(Bitmap src, Graphics dest, Size size) { PasteImageToFit(src, dest, Point.Empty, size); } public static void PasteImageToFit(Bitmap src, Graphics dest, Point location, Size size) { Size scaleSize = ExtraMath.ScaleSize(src.Size, size); Point offset = ExtraMath.PointToCenter(new Rectangle(location, size), scaleSize); dest.DrawImage(src, new Rectangle(offset, scaleSize)); } } public static class ExtraMath { public static Size ScaleSize(Size original, Size scaleTo) { if (original.Height == 0 || original.Width == 0) { //This can be a smarter special case. For most cases, //there's no reason the other dimension can't still be scaled. return Size.Empty; } double xScale = ((double)scaleTo.Width / (double)original.Width); if ((xScale * original.Height) <= scaleTo.Height) { return new Size((int)(xScale * original.Width), (int)(xScale * original.Height)); } double yScale = ((double)scaleTo.Height / (double)original.Height); return new Size((int)(yScale * original.Width), (int)(yScale * original.Height)); } public static Point RectangleCenter(Rectangle rect) { int x = rect.Left + (rect.Width / 2); int y = rect.Top + (rect.Height / 2); return new Point(x, y); } public static Point PointToCenter(Rectangle container, Size item) { Point center = RectangleCenter(container); int x = center.X - (item.Width / 2); int y = center.Y - (item.Height / 2); return new Point(x, y); } } }