0

I'm getting the out of memory exception when i put my ChromeDriver to headless mode.I haven't noticed it happening while ChromeDriver is working normally but when i put it in "headless" mode I'm getting exception.

The program is multi threaded and it should take the screenshot of captcha and convert it into base64.I've tried locking the method so only 1 thread can access but it's still happening.

 lock (this)
        {
            string fileName = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".png";
            Byte[] byteArray = ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;
            Bitmap screenshot = new Bitmap(new System.IO.MemoryStream(byteArray));
            Rectangle croppedImage = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
            //EXCEPTION ON THE LINE BELOW
            screenshot = screenshot.Clone(croppedImage, screenshot.PixelFormat);
            screenshot.Save(String.Format(fileName, ImageFormat.Png));

            byte[] imageArray = System.IO.File.ReadAllBytes(fileName);
            string base64ImageRepresentation = Convert.ToBase64String(imageArray);
            base64 = base64ImageRepresentation;
            File.Delete(fileName);
        }

0 Answers0