forked from yangzhongke/NetAutoGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenshotExtensions.cs
More file actions
81 lines (75 loc) · 2.83 KB
/
Copy pathScreenshotExtensions.cs
File metadata and controls
81 lines (75 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace NetAutoGUI
{
public static class ScreenshotExtensions
{
public static Rectangle? LocateOnScreen(this IScreenshotController ctl, BitmapData imgFileToBeFound, double confidence = 0.99)
{
var items = LocateAllOnScreen(ctl, imgFileToBeFound, confidence);
if (items.Length <= 0)
{
return null;
}
else
{
return items[0];
}
}
public static Rectangle WaitOnScreen(this IScreenshotController ctl, BitmapData imgFileToBeFound, double confidence = 0.99, double timeoutSeconds = 5)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Rectangle? rect = null;
while (sw.ElapsedMilliseconds < timeoutSeconds * 1000 && rect == null)
{
rect = LocateAllOnScreen(ctl, imgFileToBeFound, confidence).FirstOrDefault();
}
if (rect == null)
{
throw new InvalidOperationException($"image {imgFileToBeFound} not found on the screen");
}
else
{
return rect;
}
}
public static Task<Rectangle> WaitOnScreenAsync(this IScreenshotController ctl,
BitmapData imgFileToBeFound,
double confidence = 0.99, double timeoutSeconds = 5, CancellationToken cancellationToken = default)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Rectangle? rect = null;
while (sw.ElapsedMilliseconds < timeoutSeconds * 1000 && rect == null)
{
rect = LocateAllOnScreen(ctl, imgFileToBeFound, confidence).FirstOrDefault();
if (cancellationToken.IsCancellationRequested)
{
throw new TaskCanceledException();
}
}
if (rect == null)
{
throw new InvalidOperationException($"image {imgFileToBeFound} not found on the screen");
}
else
{
return Task.FromResult(rect);
}
}
public static Rectangle[] LocateAllOnScreen(this IScreenshotController ctrl, BitmapData imgFileToBeFound, double confidence = 0.99)
{
var bitmapScreen = ctrl.Screenshot();
return ctrl.LocateAll(bitmapScreen, imgFileToBeFound, confidence);
}
public static void Highlight(this IScreenshotController ctl, BitmapData imgFileToBeFound, double confidence = 0.99, double waitSeconds = 0.5)
{
var rects = LocateAllOnScreen(ctl, imgFileToBeFound, confidence);
ctl.Highlight(waitSeconds, rects);
}
}
}