using System; using System.Windows; using sharpknife.Controls; using System.Drawing; using sharpknife.Utils; using System.IO; namespace sharpknife.Commands { public class ParseUDCaptchaCommand : Command { WebBrowserControl browser; public ParseUDCaptchaCommand() : base(DateTime.Now) { this.Group = "Browser"; Timeout = 300; } public override void Execute() { base.Execute(); Bitmap captcha = ImageUtils.BitmapFromWeb("http://www.clicksia.com/udcaptcha.php"); string path = string.Format(@"..\..\Images\Captcha\UpsideDown"); DirectoryInfo directory = new DirectoryInfo(path); if (!directory.Exists) { directory.Create(); } int upside = 0; int down = 0; sharpknife.Utils.ImageUtils.MatchType[] matches = new sharpknife.Utils.ImageUtils.MatchType[6]; Image[] images = ImageUtils.Split(captcha, new Rectangle(0, 0, 48, 48)); int i = 0; foreach (Bitmap bitmap in images) { string tempImage = Path.GetTempFileName(); bitmap.Save(tempImage); Bitmap left = new Bitmap(tempImage); FileInfo[] files = directory.GetFiles(); bool found = false; bool upsideDown = true; foreach (FileInfo file in files) { Bitmap right = new Bitmap(file.FullName); Image reference = Bitmap.FromFile(file.FullName); if (ImageUtils.CompareImages(left, right)) { matches[i] = sharpknife.Utils.ImageUtils.MatchType.Upside; found = true; upside++; break; } reference.RotateFlip(RotateFlipType.Rotate180FlipNone); if (ImageUtils.CompareImages(bitmap, (Bitmap)reference)) { matches[i] = sharpknife.Utils.ImageUtils.MatchType.Down; found = true; upsideDown = false; down++; break; } right.Dispose(); } left.Dispose(); if (!found) { matches[i] = sharpknife.Utils.ImageUtils.MatchType.None; string filename = string.Format("{0}.png", Guid.NewGuid()); if (!upsideDown) { bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); } //bitmap.Save(Path.Combine(path, filename)); } i++; } UpsideDownCaptchaConfirm dialog = new UpsideDownCaptchaConfirm(); dialog.Captcha = captcha; dialog.Image1 = (Bitmap)images[0]; dialog.Image2 = (Bitmap)images[1]; dialog.Image3 = (Bitmap)images[2]; dialog.Image4 = (Bitmap)images[3]; dialog.Image5 = (Bitmap)images[4]; dialog.Image6 = (Bitmap)images[5]; dialog.Match1 = matches[0]; dialog.Match2 = matches[1]; dialog.Match3 = matches[2]; dialog.Match4 = matches[3]; dialog.Match5 = matches[4]; dialog.Match6 = matches[5]; dialog.ShowDialog(); //MessageBox.Show("Upside found: " + upside + "\nDown found: " + down); Status = StatusCode.Completed; } } }