I've a routine to take a screenshot on windows platforms with gdi plus and c++, it works perfectly with only one monitor but when I run it in a machine o VM with 2 monitors it only takes the picture one the pictures.
This it's what I'm doing:
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <GdiPlus.h>
#include <wingdi.h>
#include <fstream>
#include <unistd.h>
#pragma comment(lib, "gdiplus.lib")
#pragma comment(lib, "ws2_32.lib")
using namespace std;
// Se encarga de configurar, por asi decirlo, el formato
// de la imagen, este metodo es llamado en gdiscreen al
// momento de guardar la imagen.
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
using namespace Gdiplus;
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
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; // Success
}
}
free(pImageCodecInfo);
return 0;
}
// Este es el metodo que tomo la captura.
// c es solo una variable que utilice para probar el
// metodo en un loop, la puede quitar y remplazar c
// por el log_sec_num.
inline void take_screenshot(const std::string & filename)
{
using namespace Gdiplus;
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;
const char* name = filename.c_str();
const size_t cSize = strlen(name)+1;
wchar_t* wc = new wchar_t[cSize];
mbstowcs (wc, name, cSize);
GetEncoderClsid(L"image/png", &clsid);
bitmap.Save(wc, &clsid,NULL);
SelectObject(memdc, hOldBitmap);
DeleteObject(memdc);
DeleteObject(membit);
::ReleaseDC(0,scrdc);
}
GdiplusShutdown(gdiplusToken);
}
int main ()
{
const std::string & filename = "C:\\Screenshot.png";
take_screenshot(filename);
}
If try to get the entire screen but I end up with just have the picture. Can any one see where my error it's ??
int Height = GetSystemMetrics(SM_CYSCREEN);
Quote from the MSDN docs: "The height of the screen of the primary display monitor". You got exactly what you asked for, the primary display is one of your monitors. The first one.
Use SM_CYVIRTUALSCREEN
instead. Same for X.