public List <ImageURL> GetCaptcha() { List <ImageURL> lstImageURL = new List <ImageURL>(); string imageFilePath = currentDirectory + @"\OutputImg\"; string imageName = GetRandomImageName(); Image img = Image.FromFile(currentDirectory + @"\SourceImg\" + imageName); Bitmap bmpImg = new Bitmap(img); int width = bmpImg.Width; int height = bmpImg.Height; int widthCropSize = width / 3; int heightCropSize = height / 3; int currentX = 0; int currentY = 0; int currentImageCount = 1; int captchaId = 0; List <short> imageDegreeLst = new List <short>(); HashSet <int> excludedNumbers = GetRandomExcludedNumbers(); CaptchaSession captchaSession = new CaptchaSession { ImageName = imageName }; if (captchaSession.Insert(ref captchaId)) { for (int i = 0; i < 3; i++) { if (i != 0) { currentY += heightCropSize; } // Reset X coordinates when Y is changed currentX = 0; for (int j = 0; j < 3; j++) { if (j != 0) { currentX += widthCropSize; } Rectangle cropArea = new Rectangle(currentX, currentY, widthCropSize, heightCropSize); //cropArea.Intersect(new Rectangle(0, 0, bmpImg.Width, bmpImg.Height)); Bitmap bmpCroppedImage = bmpImg.Clone(cropArea, PixelFormat.DontCare); short rotationDegreeInt = 0; // Check if the current image is excluded from rotating or not if (!excludedNumbers.Contains(currentImageCount)) { RotateFlipType rotationDegree = GetRotationDegree(ref rotationDegreeInt); // Apply rotation bmpCroppedImage.RotateFlip(rotationDegree); } // Apply image filter Bitmap resultBitmap = ApplyRandomFilters(bmpCroppedImage); string fileName = captchaId + "-" + i + "-" + j + ".png"; //bmpCroppedImage.Save(currentDirectory + @"\OutputImg\" + fileName); //resultBitmap.Save(currentDirectory + @"\OutputImg\" + fileName); string base64String = string.Empty; using (MemoryStream stream = new MemoryStream()) { resultBitmap.Save(stream, ImageFormat.Bmp); base64String = Convert.ToBase64String(stream.ToArray()); } imageDegreeLst.Add(rotationDegreeInt); // Create imageURL image ImageURL imageURL = new ImageURL { //URL = @"http://fmcc.aquametro.com.sg/ic/OutputImg/" + fileName, Base64String = base64String, CaptchaId = captchaId }; lstImageURL.Add(imageURL); currentImageCount++; } } CaptchaAttributes captchaAttri = new CaptchaAttributes { CaptchaID = captchaId, Tile1Angle = imageDegreeLst[0], Tile2Angle = imageDegreeLst[1], Tile3Angle = imageDegreeLst[2], Tile4Angle = imageDegreeLst[3], Tile5Angle = imageDegreeLst[4], Tile6Angle = imageDegreeLst[5], Tile7Angle = imageDegreeLst[6], Tile8Angle = imageDegreeLst[7], Tile9Angle = imageDegreeLst[8] }; if (!captchaAttri.Insert()) { // Return empy list if insertion throws an error return(new List <ImageURL>()); } } excludedNumbers = null; GC.Collect(); return(lstImageURL); }
public bool CheckResult(int captchaId, string dataString) { bool result = false; int totalCaptchaTileCount = 0; CaptchaAttributes captchaAttributes = new CaptchaAttributes(); CaptchaSession captchaSession = new CaptchaSession(); captchaSession = captchaSession.GetCaptchaSession(captchaId); if (captchaSession.IsValid) { captchaAttributes = captchaAttributes.GetCaptchaAttribute_ById(captchaId); var singleImageString = dataString.Split(';'); for (int i = 0; i < singleImageString.Length; i++) { if (String.IsNullOrWhiteSpace(singleImageString[i])) { break; } // CODE OPTIMIZATION HERE if (i == 0 || result) { var imageKVP = singleImageString[i].Split('='); short currentDegree = Convert.ToInt16(imageKVP[1]); switch (imageKVP[0]) { case "captcha-1": totalCaptchaTileCount++; result = isImageDegreeCorrect(currentDegree, captchaAttributes.Tile1Angle); break; case "captcha-2": totalCaptchaTileCount++; result = isImageDegreeCorrect(currentDegree, captchaAttributes.Tile2Angle); break; case "captcha-3": totalCaptchaTileCount++; result = isImageDegreeCorrect(currentDegree, captchaAttributes.Tile3Angle); break; case "captcha-4": totalCaptchaTileCount++; result = isImageDegreeCorrect(currentDegree, captchaAttributes.Tile4Angle); break; case "captcha-5": totalCaptchaTileCount++; result = isImageDegreeCorrect(currentDegree, captchaAttributes.Tile5Angle); break; case "captcha-6": totalCaptchaTileCount++; result = isImageDegreeCorrect(currentDegree, captchaAttributes.Tile6Angle); break; case "captcha-7": totalCaptchaTileCount++; result = isImageDegreeCorrect(currentDegree, captchaAttributes.Tile7Angle); break; case "captcha-8": totalCaptchaTileCount++; result = isImageDegreeCorrect(currentDegree, captchaAttributes.Tile8Angle); break; case "captcha-9": totalCaptchaTileCount++; result = isImageDegreeCorrect(currentDegree, captchaAttributes.Tile9Angle); break; } } } // Update the captcha to invalid regardless of result captchaSession.UpdateIsValid(captchaId, false); captchaSession.DeleteCaptchaMoreThanOneDay(); //DeleteCaptchaImage(captchaId); } // If all 9 captcha tiles has not been verified if (totalCaptchaTileCount != 9) { result = false; } return(result); }