-
Notifications
You must be signed in to change notification settings - Fork 1
/
Capture.cpp
executable file
·322 lines (265 loc) · 8.07 KB
/
Capture.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "StdAfx.h"
#include "Capture.h"
#define THUMB_WSIZE 180
#define PREVIEW_WSIZE 500
map<unsigned long, string> CCapture::mProcessIdName;
map<string, string> CCapture::mWNamePName;
CCapture::CCapture(void)
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(& this->_gdiplusToken, &gdiplusStartupInput, NULL);
}
CCapture::~CCapture(void)
{
GdiplusShutdown(this->_gdiplusToken);
}
void CCapture::getScreenshot(string fname)
{
CCapture::getScreenshotWin(fname);
}
void CCapture::getSystemProcesses(string fname, string status, string debug)
{
string flist = this->getFormattedList();
if(ENABLEAES)
flist = CCrypto::aes_encryptData(flist);
COJFs::writeFile(fname, string("status: ")+ status + string("\r\n") +flist + string("\r\n") + debug);
}
void CCapture::_getScreenshotWinAll(string fname)
{
EnumDisplayMonitors(NULL, NULL, CCapture::monitorEnumProc, (LPARAM)fname.c_str());
}
string CCapture::getScreenshotWin(string fname)
{
int nScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,
nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,
hDesktopDC,0,0,SRCCOPY|CAPTUREBLT);
this->_SaveImageW(hCaptureBitmap, fname);
ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);
return fname;
}
int CCapture::_SaveImage(Bitmap *bm, string sFilename, int maxWidth)
{
try
{
CLSID pngClsid;
int newHeight = bm->GetHeight();
int newWidth = bm->GetWidth();
Gdiplus::Bitmap * bmr = bm;
bool resized = false;
// resize if necessary
if(maxWidth == 0)
{
maxWidth = this->_standardWidth;
}
if(maxWidth != 0 && newWidth > maxWidth)
{
newWidth = maxWidth;
newHeight = (int)(maxWidth*(bm->GetHeight()/bm->GetWidth()));
bmr = this->_ResizeClone(bm, newWidth, newHeight);
resized = true;
}
else
{
bm = NULL;
}
if(sFilename.find(".png") != sFilename.npos)
this->_GetEncoderClsid(L"image/png", &pngClsid);
if(sFilename.find(".jpg") != sFilename.npos)
this->_GetEncoderClsid(L"image/jpeg", &pngClsid);
if(sFilename.find(".gif") != sFilename.npos)
this->_GetEncoderClsid(L"image/gif", &pngClsid);
EncoderParameters encoderParameters;
ULONG quality;
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
quality = 80; // quality
encoderParameters.Parameter[0].Value = &quality;
wchar_t ws[200];
swprintf(ws, 200, L"%hs", sFilename.c_str());
bmr->Save(ws, &pngClsid, &encoderParameters);
if(resized)
delete(bmr);
bmr = NULL;
return 1;
}
catch(...)
{
return 0;
}
}
int CCapture::_SaveImageW(HBITMAP hBmp, string sFilename, int maxWidth)
{
Gdiplus::Bitmap bm(hBmp, NULL);
return this->_SaveImage(&bm, sFilename, maxWidth);
}
void CCapture::createThumbs(string fname, string _tm, string _prev)
{
wchar_t ws[200];
swprintf(ws, 200, L"%hs", fname.c_str());
Gdiplus::Bitmap bm(ws);
string x = this->fileMinusX(fname);
string tm = fname + _tm + x;
string pr = fname + _prev + x;
this->_SaveImage(&bm, tm, THUMB_WSIZE);
this->_SaveImage(&bm, pr, PREVIEW_WSIZE);
}
Bitmap* CCapture::_ResizeClone(Bitmap *bmp, INT width, INT height)
{
UINT o_height = bmp->GetHeight();
UINT o_width = bmp->GetWidth();
INT n_width = width;
INT n_height = height;
double ratio = ((double)o_width) / ((double)o_height);
if (o_width > o_height) {
// Resize down by width
n_height = static_cast<UINT>(((double)n_width) / ratio);
} else {
n_width = static_cast<UINT>(n_height * ratio);
}
Gdiplus::Bitmap* newBitmap = new Gdiplus::Bitmap(n_width, n_height, bmp->GetPixelFormat());
Gdiplus::Graphics graphics(newBitmap);
graphics.DrawImage(bmp, 0, 0, n_width, n_height);
return newBitmap;
}
int CCapture::_GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return 0;
}
string CCapture::fileMinusX(string& fname)
{
size_t pos = fname.rfind(".");
if(pos != fname.npos)
{
string x = fname.substr(pos);
fname = fname.substr(0, pos);
return x;
}
return "";
}
string CCapture::getFormattedList()
{
CCapture::mProcessIdName.clear();
CCapture::mWNamePName.clear();
this->getProcesses();
this->getWindowsList();
string ret = "";
map<string, string>::iterator it;
for(it = CCapture::mWNamePName.begin(); it != CCapture::mWNamePName.end(); it++)
{
string pretty = it->first + " - " + it->second;
ret += pretty + "\r\n";
}
return ret;
}
vector<string> CCapture::getProcesses()
{
unsigned long aProcesses[1024], cbNeeded, cProcesses;
vector<string> rprocess;
unsigned int i;
if(!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return rprocess;
}
cProcesses = cbNeeded / sizeof(DWORD);
for ( i = 0; i < cProcesses; i++ )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, aProcesses[i] );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
rprocess.push_back(string(szProcessName));
CCapture::mProcessIdName.insert(make_pair(aProcesses[i], string(szProcessName)));
}
}
//rprocess.push_back(string(szProcessName));
CloseHandle( hProcess );
}
return rprocess;
}
vector<string> CCapture::getWindows()
{
vector<string> windows;
EnumDesktopWindows(NULL, CCapture::EnumWindowsProc, (LPARAM) &windows);
return windows;
}
string CCapture::getWindowsList()
{
string retString = "";
vector<string> windows = this->getWindows();
for (vector<string>::iterator it = windows.begin() ; it != windows.end(); ++it)
{
if(*it != "<unknown>")
{
retString += *it;
retString += "\n";
}
}
return retString;
}
BOOL CALLBACK CCapture::EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
vector<string> & windows = *(vector<string>*)lParam;
char title[256];
GetWindowText(hwnd, title, 256);
if(string(title) != "")
{
windows.push_back(string(title));
unsigned long pid;
GetWindowThreadProcessId(hwnd, &pid);
if(CCapture::mProcessIdName.find(pid) != CCapture::mProcessIdName.end())
{
CCapture::mWNamePName.insert(make_pair(string(title), CCapture::mProcessIdName[pid]));
}
}
return TRUE;
}
BOOL CALLBACK CCapture::monitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData )
{
CApp::Engine()->debug_("monitor enum");
string fname = (const char *)dwData;
CApp::Capture.getScreenshotWin(fname);
return TRUE;
}