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); }