-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot.cpp
More file actions
97 lines (70 loc) · 1.95 KB
/
Copy pathscreenshot.cpp
File metadata and controls
97 lines (70 loc) · 1.95 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
#include "Screenshot.h"
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib,"gdiplus.lib")
using namespace Gdiplus;
Screenshot::Screenshot()
{
}
Screenshot::~Screenshot()
{
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0;
UINT size = 0;
GetImageEncodersSize(&num, &size);
if (size == 0)
return -1;
ImageCodecInfo* pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
return -1;
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;
}
}
free(pImageCodecInfo);
return -1;
}
void Screenshot::CaptureScreen()
{
HWND hwnd = GetDesktopWindow();
HDC hdcScreen = GetDC(hwnd);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
int width = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, width, height);
SelectObject(hdcMem, hBitmap);
BitBlt(
hdcMem,
0,
0,
width,
height,
hdcScreen,
0,
0,
SRCCOPY
);
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Bitmap bitmap(hBitmap, NULL);
CreateDirectory(L"Screenshots", NULL);
static int count = 1;
WCHAR fileName[MAX_PATH];
wsprintf(fileName, L"Screenshots\\Screenshot_%d.png", count++);
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
bitmap.Save(fileName, &pngClsid, NULL);
DeleteObject(hBitmap);
DeleteDC(hdcMem);
ReleaseDC(hwnd, hdcScreen);
//GdiplusShutdown(gdiplusToken);
}