Skip to content

Instantly share code, notes, and snippets.

@dave5623
Last active June 27, 2017 03:31
Show Gist options
  • Save dave5623/f064f84943c67a80de7068e80b1f55ca to your computer and use it in GitHub Desktop.
Save dave5623/f064f84943c67a80de7068e80b1f55ca to your computer and use it in GitHub Desktop.
JPEG Screenshot RDLL
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "Screenshot.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
takeScreenshot();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// 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 <iostream>
#include <Windows.h>
#include <objidl.h>
#include <gdiplus.h>
#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;
}
#pragma once
#ifdef SCREENSHOT_EXPORTS
#define SCREENSHOT extern "C" __declspec(dllexport)
#else
#define SCREENSHOT extern "C" __declspec(dllimport)
#endif
SCREENSHOT int takeScreenshot();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment