Adrianeloy,
Dasije, aquí está el fuente:
Código CPP:
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
#define DllExport __declspec( dllexport )
extern "C" { // Undecorate
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
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 -1; // Failure
}
DllExport int HazPantallazo(const char* SavePath)
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// get the device context of the screen
HDC hScreenDC
= CreateDC("DISPLAY",
NULL,
NULL,
NULL); // and a device context to put it in
// maybe worth checking these are positive values
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
// get a new bitmap
HBITMAP hOldBitmap
= SelectObject(hMemoryDC,
(HGDIOBJ
)hBitmap
);
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
Bitmap *image = new Bitmap(hBitmap, NULL);
CLSID myClsId;
int retVal = GetEncoderClsid(L"image/jpg", &myClsId);
wchar_t PATH[512];
const wchar_t* WSavePath = &PATH[0];
mbstowcs(PATH, SavePath, 512);
image->Save(WSavePath, &myClsId, NULL);
delete image;
GdiplusShutdown(gdiplusToken);
// clean up
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
return 0;
}
} // Undecorate
Según he leído, se puede indicar la calidad (nivel de compresión) en el 3-er parámetro en la invocación del método
save (donde va el NULL) :
Código CPP:
image->Save(WSavePath, &myClsId, NULL);
Pero no sé por qué, no me compila en DevC++ con la estructura
EncoderParameter.
Adrianeloy, el formato depende de lo que tú indiques. puede ser:
Código CPP:
int retVal = GetEncoderClsid(L"image/jpg", &myClsId);
int retVal = GetEncoderClsid(L"image/bmp", &myClsId);
int retVal = GetEncoderClsid(L"image/png", &myClsId);
Lo he modificado a JPG porque el BMP por defecto pesa 6 megas cada pantallazo
