// Screenshot.cpp : Defines the exported functions for the DLL application. // // Taken from https://gist.github.com/ebonwheeler/3865787 // Add gdiplus.lib as a Linker > Input > Additional Dependencies #include "stdafx.h" #include #include #include #include #pragma comment(lib,"Gdiplus.lib") using namespace Gdiplus; using namespace std; #define SCREENSHOT_EXPORTS #include "Screenshot.h" int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { UINT num = 0; UINT size = 0; ImageCodecInfo* pImageCodecInfo = NULL; GetImageEncodersSize(&num, &size); if(size == 0) { return -1; } 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; } } return -1; } void BitmapToJpg(HBITMAP hbmpImage, int width, int height) { Bitmap *p_bmp = Bitmap::FromHBITMAP(hbmpImage, NULL); CLSID pngClsid; int result = GetEncoderClsid(L"image/jpeg", &pngClsid); if (result != -1) { std::cout << "Encoder succeeded" << std::endl; } else { std::cout << "Encoder failed" << std::endl; } p_bmp->Save(L"c:\\windows\\temp\\screen.jpg", &pngClsid, NULL); delete p_bmp; } bool ScreenCapture(int x, int y, int width, int height, char *filename) { HDC hDc = CreateCompatibleDC(0); HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), width, height); SelectObject(hDc, hBmp); BitBlt(hDc, 0, 0, width, height, GetDC(0), x, y, SRCCOPY); BitmapToJpg(hBmp, width, height); DeleteObject(hBmp); return true; } int takeScreenshot() { GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); int x1 = 0; int y1 = 0; int x2 = GetSystemMetrics(SM_CXSCREEN); int y2 = GetSystemMetrics(SM_CYSCREEN); ScreenCapture(x1, y1, x2 - x1, y2 - y1, "screen.jpg"); GdiplusShutdown(gdiplusToken); return 0; }