Skip to content

Instantly share code, notes, and snippets.

@dbdeadka
Forked from prashanthrajagopal/screenshot.cpp
Created November 8, 2018 19:38
Show Gist options
  • Save dbdeadka/ba1fde831ac096d52be3d8d1a5cd0a47 to your computer and use it in GitHub Desktop.
Save dbdeadka/ba1fde831ac096d52be3d8d1a5cd0a47 to your computer and use it in GitHub Desktop.
Take a screenshot and save as jpeg in c++
#include <stdio.h>
#include <windows.h>
#include <gdiplus.h>
#include <time.h>
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
using namespace Gdiplus;
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;
}
}
free(pImageCodecInfo);
return 0;
}
void gdiscreen() {
using namespace Gdiplus;
IStream* istream;
HRESULT res = CreateStreamOnHGlobal(NULL, true, &istream);
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
{
HDC scrdc, memdc;
HBITMAP membit;
scrdc = ::GetDC(0);
int Height = GetSystemMetrics(SM_CYSCREEN);
int Width = GetSystemMetrics(SM_CXSCREEN);
memdc = CreateCompatibleDC(scrdc);
membit = CreateCompatibleBitmap(scrdc, Width, Height);
HBITMAP hOldBitmap =(HBITMAP) SelectObject(memdc, membit);
BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
Gdiplus::Bitmap bitmap(membit, NULL);
CLSID clsid;
GetEncoderClsid(L"image/jpeg", &clsid);
// bitmap.Save(L"screen.jpeg", &clsid, NULL); // To save the jpeg to a file
bitmap.Save(istream, &clsid, NULL);
// Create a bitmap from the stream and save it to make sure the stream has the image
// Gdiplus::Bitmap bmp(istream, NULL);
// bmp.Save(L"t1est.jpeg", &clsid, NULL);
// END
delete &clsid;
DeleteObject(memdc);
DeleteObject(membit);
::ReleaseDC(0,scrdc);
}
GdiplusShutdown(gdiplusToken);
istream->Release();
}
int main()
{
clock_t t1 = clock();
int i;
int iterations = 10;
for(i=0;i<iterations;i++){
gdiscreen();
}
clock_t t2 = clock();
printf("%d iterations: %0.0f fps\n", iterations, iterations/((double)(t2 - t1) / CLOCKS_PER_SEC));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment