File size: 2,439 Bytes
68dd90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
using System.Drawing;
using System.Windows;
using Point = System.Windows.Point;

namespace ScreenGrab.Extensions;

public static class ShapeExtensions
{
    public static Rect AsRect(this Rectangle rectangle)
    {
        return new Rect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
    }

    public static Rectangle AsRectangle(this Rect rect)
    {
        return new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);
    }

    public static Rect GetScaledDownByDpi(this Rect rect, DpiScale dpi)
    {
        return new Rect(rect.X / dpi.DpiScaleX,
            rect.Y / dpi.DpiScaleY,
            rect.Width / dpi.DpiScaleX,
            rect.Height / dpi.DpiScaleY);
    }

    public static Rect GetScaledUpByDpi(this Rect rect, DpiScale dpi)
    {
        return new Rect(rect.X * dpi.DpiScaleX,
            rect.Y * dpi.DpiScaleY,
            rect.Width * dpi.DpiScaleX,
            rect.Height * dpi.DpiScaleY);
    }

    public static Rect GetScaledUpByFraction(this Rect rect, double scaleFactor)
    {
        return new Rect(rect.X * scaleFactor,
            rect.Y * scaleFactor,
            rect.Width * scaleFactor,
            rect.Height * scaleFactor);
    }

    public static Rect GetScaleSizeByFraction(this Rect rect, double scaleFactor)
    {
        return new Rect(rect.X,
            rect.Y,
            rect.Width * scaleFactor,
            rect.Height * scaleFactor);
    }

    public static bool IsGood(this Rect rect)
    {
        if (double.IsNaN(rect.X)
            || double.IsNegativeInfinity(rect.X)
            || double.IsPositiveInfinity(rect.X))
            return false;

        if (double.IsNaN(rect.Y)
            || double.IsNegativeInfinity(rect.Y)
            || double.IsPositiveInfinity(rect.Y))
            return false;

        if (double.IsNaN(rect.Height)
            || rect.Height == 0
            || double.IsNegativeInfinity(rect.Height)
            || double.IsPositiveInfinity(rect.Height))
            return false;

        if (double.IsNaN(rect.Width)
            || rect.Width == 0
            || double.IsNegativeInfinity(rect.Width)
            || double.IsPositiveInfinity(rect.Width))
            return false;

        return true;
    }

    public static Point CenterPoint(this Rect rect)
    {
        var x = rect.Left + rect.Width / 2;
        var y = rect.Top + rect.Height / 2;
        return new Point(x, y);
    }
}