Я пытаюсь выполнить очень сложную операцию параллельно с TPL For. У этого есть API, вызывающий с async / await и с селеновым webdriver.
Когда я отлаживаю приложение, оно работает гладко, но когда я запускаю его без отладчика, оно вскоре вылетает, и появляется следующее сообщение.
{System.OutOfMemoryException: Out of memory.
at System.Drawing.Bitmap.Clone(Rectangle rect, PixelFormat format)
at Browser_Automation.Parser.CropImage(Bitmap source, Rectangle section) in E:\Work\XXXXX\XXXXX\XXXXX Automation\JSON\SuiteParser.cs:line 598
at XXXXX.Parser.GetCaptchaById(IWebDriver driver, String id) in E:\Work\XXXXX\XXXXX\XXXXXAutomation\JSON\SuiteParser.cs:line 577
at Browser_Automation.Parser.<GetFirstCaptchaUrl>d__23.MoveNext() in E:\Work\XXXXX\XXXXX\XXXXXAutomation\JSON\SuiteParser.cs:line 526
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Browser_Automation.Parser.<_Execute>d__27.MoveNext() in E:\Work\SupportKing\VisaBot\Browser Automation\JSON\SuiteParser.cs:line 646}
Вот код, где он указывает на память растрового изображения ....
private string GetCaptchaById(IWebDriver driver, string id)
{
//Getting the image, which is in this case has a url as source.
var captcha_image = driver.FindElement(By.Id(id));
string resultBase64 = "";
if (captcha_image != null)
{
try
{
//Deleting previous screenshot images from the folder
System.IO.DirectoryInfo di = new DirectoryInfo(App.Directory.FullName + "\\ScreenshotImages");
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
}
catch (Exception ex)
{
}
var path1 = App.Directory.FullName + "\\ScreenshotImages\\" + Guid.NewGuid().ToString() + ".png";
//Creating screenshot of the full web page and saving it to the path
ITakesScreenshot ssdriver = driver as ITakesScreenshot;
Screenshot screenshot = ssdriver.GetScreenshot();
Screenshot tempImage = screenshot;
tempImage.SaveAsFile(path1, ScreenshotImageFormat.Png);
//Creating a rectangle of same size as the captcha
Point point = captcha_image.Location;
point.X = point.X;
point.Y = point.Y;
int width = captcha_image.Size.Width;
int height = captcha_image.Size.Height;
Rectangle section = new Rectangle(point, new System.Drawing.Size(width, height));
//Loding the full page image from the path
Bitmap fullImg = new Bitmap(path1);
//creating a new image as the same width height of the captcha
Bitmap source = new System.Drawing.Bitmap(width, height);
//This is the final captcha image. We are croping the captcha image from the full image with location(X,Y) and size(W,H)
Bitmap final_image = CropImage(fullImg, section);
resultBase64 = ConvertBitmapToBase64(final_image);
//final_image.Save(@"E:\\out.jpg");
}
return resultBase64;
}
И это строка, где ее ловит ошибка:
Screenshot screenshot = ssdriver.GetScreenshot();