-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint _GetEncoderClsid(const WCHAR fo.txt
151 lines (129 loc) · 5.25 KB
/
int _GetEncoderClsid(const WCHAR fo.txt
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
describe('window.captureScreen', () => {
it('captures the screen and saves to the specified file path', async () => {
runner.run(`
await Neutralino.window.captureScreen("screenshot.png");
await __close('done');
`);
assert.equal(runner.getOutput(), 'done');
});
it('throws an error for missing file path parameter', async () => {
runner.run(`
try {
await Neutralino.window.captureScreen();
} catch (err) {
await __close(err.code);
}
`);
assert.equal(runner.getOutput(), 'NE_RT_NATRTER');
});
});
describe('window.setTitle', () => {
it('works without parameters', async () => {
runner.run(`
await Neutralino.window.setTitle();
await __close('done');
`);
assert.equal(runner.getOutput(), 'done');
});
});
#if defined(_WIN32)
int _GetEncoderClsid(const WCHAR *format, CLSID *pClsid) {
UINT num = 0; // Number of image encoders
UINT size = 0; // Size of the image encoder array in bytes
Gdiplus::GetImageEncodersSize(&num, &size);
if (size == 0) return -1; // Failure
Gdiplus::ImageCodecInfo *pImageCodecInfo = (Gdiplus::ImageCodecInfo *)(malloc(size));
if (pImageCodecInfo == NULL) return -1; // Failure
Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);
for (UINT i = 0; i < num; ++i) {
if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0) {
*pClsid = pImageCodecInfo[i].Clsid;
free(pImageCodecInfo);
return i; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
#endif
void captureScreen(const std::string &outputFile) {
#if defined(__linux__) || defined(__FreeBSD__) // Working Fine Verifeid
if (!gtk_init_check(0, nullptr)) {
std::cerr << "GTK initialization failed." << std::endl;
return;
}
// Get the application's window handle
GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(windowHandle));
if (!window) {
std::cerr << "Unable to get the application window." << std::endl;
return;
}
// Get window geometry (width, height)
int width, height;
gdk_window_get_geometry(window, nullptr, nullptr, &width, &height);
// Capture the application window
GdkPixbuf *screenshot = gdk_pixbuf_get_from_window(window, 0, 0, width, height);
if (!screenshot) {
std::cerr << "Failed to capture the application window." << std::endl;
return;
}
// Save screenshot to file
GError *error = nullptr;
if (!gdk_pixbuf_save(screenshot, outputFilePath.c_str(), "png", &error, nullptr)) {
std::cerr << "Failed to save screenshot: " << error->message << std::endl;
g_error_free(error);
} else {
std::cout << "Screenshot saved to: " << outputFilePath << std::endl;
}
// Free resources
g_object_unref(screenshot);
#elif defined(__APPLE__)
// macOS implementation not done
#elif defined(_WIN32) // Working Fine Verifeid
SetProcessDPIAware();
// Initialize GDI+
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
if (Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr) != Gdiplus::Ok) {
std::cerr << "Error: Failed to initialize GDI+." << std::endl;
return;
}
// Get window dimensions
RECT rect;
GetWindowRect(windowHandle, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
// Create a compatible DC and bitmap
HDC hdcWindow = GetDC(windowHandle);
HDC hdcMem = CreateCompatibleDC(hdcWindow);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcWindow, width, height);
SelectObject(hdcMem, hBitmap);
// Use PrintWindow for more accurate rendering
if (!PrintWindow(windowHandle, hdcMem, PW_RENDERFULLCONTENT)) {
std::cerr << "Warning: PrintWindow failed. Falling back to BitBlt." << std::endl;
// Fallback to BitBlt
if (!BitBlt(hdcMem, 0, 0, width, height, hdcWindow, 0, 0, SRCCOPY)) {
std::cerr << "Error: BitBlt also failed." << std::endl;
DeleteObject(hBitmap);
DeleteDC(hdcMem);
ReleaseDC(windowHandle, hdcWindow);
Gdiplus::GdiplusShutdown(gdiplusToken);
return;
}
}
// Save the bitmap using GDI+
Gdiplus::Bitmap bitmap(hBitmap, nullptr);
CLSID clsid;
if (_GetEncoderClsid(L"image/png", &clsid) == -1) {
std::cerr << "Error: PNG encoder not found." << std::endl;
} else {
std::wstring ws(outputFile.begin(), outputFile.end());
Gdiplus::Status status = bitmap.Save(ws.c_str(), &clsid, nullptr);
if (status != Gdiplus::Ok) {
std::cerr << "Error: Failed to save the screenshot. GDI+ status: " << status << std::endl;
} else {
std::cout << "Screenshot saved to: " << outputFile << std::endl;
}
}
#endif
}