-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenGrabber.cpp
More file actions
110 lines (87 loc) · 2.64 KB
/
ScreenGrabber.cpp
File metadata and controls
110 lines (87 loc) · 2.64 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "ScreenGrabber.h"
#include "StreamOnHGlobal.h"
#include <memory>
std::optional<CLSID> CScreenGrabber::GetEncoderClsid(const std::wstring& mimeType) {
UINT num = 0, size = 0;
Gdiplus::GetImageEncodersSize(&num, &size);
if (size == 0) {
return std::nullopt;
}
std::unique_ptr<uint8_t[]> encoders(new uint8_t[size]);
Gdiplus::GetImageEncoders(num, size, (Gdiplus::ImageCodecInfo*)encoders.get());
for (size_t i = 0; i < num; ++i) {
auto& encoder = *((Gdiplus::ImageCodecInfo*)encoders.get() + i);
if (encoder.MimeType == mimeType) {
return encoder.Clsid;
}
}
return std::nullopt;
}
CScreenGrabber::CScreenGrabber() : encoder({}), encoderParameters({}) {
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
}
CScreenGrabber::~CScreenGrabber() {
Gdiplus::GdiplusShutdown(gdiplusToken);
}
void CScreenGrabber::SetEncoder(const CLSID& clsid) {
encoder = clsid;
}
bool CScreenGrabber::SetEncoder(const std::wstring& mimeType) {
std::optional<CLSID> clsid = GetEncoderClsid(mimeType);
if (!clsid.has_value()) {
return false;
}
SetEncoder(clsid.value());
return true;
}
void CScreenGrabber::SetEncoderParameters(const Gdiplus::EncoderParameters& newEncoderParameters) {
encoderParameters = newEncoderParameters;
}
std::optional<std::vector<uint8_t>> CScreenGrabber::ScreenCapture(LONG left, LONG top, LONG width, LONG height) {
HDC hDc = GetDC(nullptr);
if (!hDc) {
return std::nullopt;
}
HDC hBitmapDC = nullptr;
HBITMAP hBitmap = nullptr;
HBITMAP hOldBitmap = nullptr;
Gdiplus::Bitmap* bitmap = nullptr;
CStreamOnHGlobal stream = {};
std::optional<std::vector<uint8_t>> result = std::nullopt;
hBitmapDC = CreateCompatibleDC(hDc);
if (!hBitmapDC) {
goto cleanup_dc;
}
hBitmap = CreateCompatibleBitmap(hDc, width, height);
if (!hBitmap) {
goto cleanup_bitmap_dc;
}
hOldBitmap = (HBITMAP)SelectObject(hBitmapDC, hBitmap);
if (!BitBlt(hBitmapDC, 0, 0, width, height, hDc, left, top, SRCCOPY)) {
goto cleanup_bitmap;
}
bitmap = Gdiplus::Bitmap::FromHBITMAP(hBitmap, nullptr);
if (!bitmap) {
goto cleanup_bitmap;
}
if (bitmap->Save(stream.GetStream(), &encoder, &encoderParameters) != Gdiplus::Ok) {
goto cleanup_gdiplus;
}
result = stream.ReadAll();
if (!result) {
goto cleanup_gdiplus;
}
cleanup_gdiplus:
delete bitmap;
cleanup_bitmap:
SelectObject(hBitmapDC, hOldBitmap);
DeleteObject(hBitmap);
cleanup_bitmap_dc:
DeleteDC(hBitmapDC);
cleanup_dc:
ReleaseDC(nullptr, hDc);
return result;
}
std::optional<std::vector<uint8_t>> CScreenGrabber::ScreenCapture(const SMonitorInfo& monitorInfo) {
return ScreenCapture(monitorInfo.left, monitorInfo.top, monitorInfo.width, monitorInfo.height);
}