File size: 3,049 Bytes
68dd90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cd514d
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ScreenGrab.Utilities;
using PixelFormat = System.Drawing.Imaging.PixelFormat;

namespace ScreenGrab.Extensions;

public static class ImageExtensions
{
    public static ImageSource? GetWindowBoundsImage(this Window passedWindow)
    {
        var bmp = passedWindow.GetWindowsBoundsBitmap();
        return bmp.ToImageSource();
    }

    public static Tuple<double, double> GetWidthHeight(this Window window)
    {
        var dpi = VisualTreeHelper.GetDpi(window);
        return new Tuple<double, double>(window.ActualWidth * dpi.DpiScaleX, window.ActualHeight * dpi.DpiScaleY);
    }

    public static Bitmap GetWindowsBoundsBitmap(this Window passedWindow)
    {
        var (windowWidth, windowHeight) = passedWindow.GetWidthHeight();

        var absPosPoint = passedWindow.GetAbsolutePosition();

        var thisCorrectedLeft = (int)absPosPoint.X;
        var thisCorrectedTop = (int)absPosPoint.Y;

        Bitmap bmp = new((int)windowWidth, (int)windowHeight, PixelFormat.Format32bppArgb);
        using var g = Graphics.FromImage(bmp);

        g.CopyFromScreen(thisCorrectedLeft, thisCorrectedTop, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
        return bmp;
    }

    public static BitmapImage? ToImageSource(this Bitmap? bitmap)
    {
        if (bitmap == null) return default;
        using MemoryStream memory = new();
        using WrappingStream wrapper = new(memory);

        bitmap.Save(wrapper, ImageFormat.Bmp);
        wrapper.Position = 0;
        BitmapImage bitmapImage = new();
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = wrapper;
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.EndInit();
        bitmapImage.StreamSource = null;
        bitmapImage.Freeze();

        memory.Flush();
        wrapper.Flush();

        return bitmapImage;
    }

    public static Bitmap GetRegionOfScreenAsBitmap(this Rectangle region)
    {
        if (region.Width <= 1 || region.Height <= 1)
        {
            return null;
        }

        Bitmap bmp = new(region.Width, region.Height, PixelFormat.Format32bppArgb);
        using var g = Graphics.FromImage(bmp);

        g.CopyFromScreen(region.Left, region.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
        bmp = bmp.PadImage();

        return bmp;
    }

    public static Bitmap PadImage(this Bitmap image, int minW = 64, int minH = 64)
    {
        if (image.Height >= minH && image.Width >= minW)
            return image;

        var width = Math.Max(image.Width + 16, minW + 16);
        var height = Math.Max(image.Height + 16, minH + 16);

        // Create a compatible bitmap
        Bitmap destination = new(width, height, image.PixelFormat);
        using var gd = Graphics.FromImage(destination);

        gd.Clear(image.GetPixel(0, 0));
        gd.DrawImageUnscaled(image, 8, 8);

        return destination;
    }
}