099c49f7b3c3892fcb984f59831d719fd20830eb
[public/netxms.git] / src / agent / nxsagent / screenshot.cpp
1 /*
2 ** NetXMS Session Agent
3 ** Copyright (C) 2003-2014 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be usefu,,
11 ** but ITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: screenshot.cpp
20 **
21 **/
22
23 #include "nxsagent.h"
24
25 /**
26 * Take screenshot
27 */
28 void TakeScreenshot(NXCPMessage *response)
29 {
30 UINT32 rcc = ERR_INTERNAL_ERROR;
31
32 HDC dc = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
33 if (dc != NULL)
34 {
35 HDC memdc = CreateCompatibleDC(dc);
36 if (memdc != NULL)
37 {
38 int x = GetSystemMetrics(SM_XVIRTUALSCREEN);
39 int y = GetSystemMetrics(SM_YVIRTUALSCREEN);
40 int cx = GetSystemMetrics(SM_CXVIRTUALSCREEN);
41 int cy = GetSystemMetrics(SM_CYVIRTUALSCREEN);
42
43 HBITMAP bitmap = CreateCompatibleBitmap(dc, cx, cy);
44 if (bitmap != NULL)
45 {
46 SelectObject(memdc, bitmap);
47 BitBlt(memdc, 0, 0, cx, cy, dc, x, y, SRCCOPY | CAPTUREBLT);
48 }
49
50 DeleteDC(memdc);
51
52 TCHAR tempPath[MAX_PATH];
53 GetTempPath(MAX_PATH, tempPath);
54
55 TCHAR tempFile[MAX_PATH];
56 GetTempFileName(tempPath, _T("nx"), 0, tempFile);
57 if (SaveBitmapToPng(bitmap, tempFile))
58 {
59 rcc = ERR_SUCCESS;
60 response->setFieldFromFile(VID_FILE_DATA, tempFile);
61 }
62 else
63 {
64 AgentWriteDebugLog(4, _T("Cannot save bitmap as PNG"));
65 }
66 DeleteObject(bitmap);
67 DeleteFile(tempFile);
68 }
69 DeleteDC(dc);
70 }
71
72 response->setField(VID_RCC, rcc);
73 }