I have a method which takes in the webcontrol and other parameters in order to take a screen shot of the webcontrol every 200 ms then i have another method which finds a small picture in the screenshot and if the method finds it, a true is returned.
The goal:
take a screenshot while the webcontrol loads during a specific timespan.
The issue:
the webcontrol and the form UI freeze.
I tried a different thread but i get an exception as the webcontrol was created in a different thread.
The code:
private bool Exists()
{
bool exists= findImage.imageExists(conn.imgLoaded, 30,FUTWebControl).Result.Item1;
return exists;
}
public async Task< Tuple<bool, Point>>imageExists(String IconPath, Double timeSpan, WebControl webControl)
{
int waitTime = Convert.ToInt32(timeSpan * 1000);
Tuple<bool, Point> imageFound = new Tuple<bool, Point>(false, new Point(0, 0));
Stopwatch timer = new Stopwatch();
timer.Start();
while (true)
{
await Task.Delay(200);
imageFound = imageExists(IconPath,webControl);
if (imageFound.Item1 || timer.ElapsedMilliseconds >= waitTime)
break;
}
return imageFound;
}
public Tuple<bool, Point> imageExists(String IconPath, WebControl webControl)
{
Tuple<Point, Size> locSize = getIconLocation(IconPath,webControl);
if (locSize.Item1 != new Point(0, 0))
return new Tuple<bool, Point>(true, getClickLocation(locSize));
return new Tuple<bool, Point>(false, getClickLocation(locSize));
}
private Tuple<Point, Size> getIconLocation(String IconPath, WebControl webControl)
{
Point location = new Point();
Size imgSize = new Size(0, 0);
using (Bitmap bmpScreenCapture = new Bitmap(webControl.Width, webControl.Height))
{
webControl.DrawToBitmap(bmpScreenCapture, new Rectangle(0, 0, webControl.Width, webControl.Height));
using (Bitmap sub = new Bitmap(IconPath))
{
location = getsubLocation(bmpScreenCapture, sub);
bmpScreenCapture.Save(@"C:\screenshot.bmp", ImageFormat.Bmp);
imgSize = sub.Size;
}
}
return new Tuple<Point, Size>(location, imgSize);
}