Ver Mensaje Individual
  #1
Antiguo 9 de marzo de 2017, 13:48
Kuk
Administrador
Última Actividad 19.09.2026 20:11
Posts Posts: 2.500
Likes enviados Enviados: 1037
Likes recibidos Recibidos: 1207
Foto DLL que crea un pantallazo
1 Inactivo Inactivo

Adrianeloy, Dasije, aquí está el fuente:

Código CPP:
  1. #include <windows.h>
  2. #include <gdiplus.h>
  3. using namespace Gdiplus;
  4.  
  5. #define DllExport __declspec( dllexport )
  6.  
  7. extern "C" { // Undecorate
  8.  
  9. int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
  10. {
  11.    UINT  num = 0;          // number of image encoders
  12.    UINT  size = 0;         // size of the image encoder array in bytes
  13.  
  14.    ImageCodecInfo* pImageCodecInfo = NULL;
  15.  
  16.    GetImageEncodersSize(&num, &size);
  17.    if(size == 0)
  18.       return -1;  // Failure
  19.  
  20.    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  21.    if(pImageCodecInfo == NULL)
  22.       return -1;  // Failure
  23.  
  24.    GetImageEncoders(num, size, pImageCodecInfo);
  25.  
  26.    for(UINT j = 0; j < num; ++j)
  27.    {
  28.       if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
  29.       {
  30.          *pClsid = pImageCodecInfo[j].Clsid;
  31.          free(pImageCodecInfo);
  32.          return j;  // Success
  33.       }
  34.    }
  35.    free(pImageCodecInfo);
  36.    return -1;  // Failure
  37. }
  38.  
  39. DllExport int HazPantallazo(const char* SavePath)
  40. {
  41.     GdiplusStartupInput gdiplusStartupInput;
  42.     ULONG_PTR gdiplusToken;
  43.     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  44.  
  45. // get the device context of the screen
  46. HDC hScreenDC = CreateDC("DISPLAY", NULL, NULL, NULL);    
  47. // and a device context to put it in
  48. HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
  49.  
  50. int width = GetDeviceCaps(hScreenDC, HORZRES);
  51. int height = GetDeviceCaps(hScreenDC, VERTRES);
  52.  
  53. // maybe worth checking these are positive values
  54. HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
  55.  
  56. // get a new bitmap
  57. HBITMAP hOldBitmap = SelectObject(hMemoryDC, (HGDIOBJ)hBitmap);
  58.  
  59. BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
  60. hBitmap = SelectObject(hMemoryDC, (HGDIOBJ)hOldBitmap);
  61.    
  62.     Bitmap *image = new Bitmap(hBitmap, NULL);
  63.    
  64.     CLSID myClsId;
  65.     int retVal = GetEncoderClsid(L"image/jpg", &myClsId);
  66.    
  67. wchar_t  PATH[512];    
  68. const wchar_t* WSavePath = &PATH[0];    
  69.    
  70. mbstowcs(PATH, SavePath, 512);
  71.    
  72.     image->Save(WSavePath, &myClsId, NULL);
  73.     delete image;
  74.  
  75.     GdiplusShutdown(gdiplusToken);    
  76.     // clean up
  77. DeleteDC(hMemoryDC);
  78. DeleteDC(hScreenDC);
  79.  
  80.     return 0;
  81. }
  82.  
  83. } // 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:
  1. 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:
  1. int retVal = GetEncoderClsid(L"image/jpg", &myClsId);
  2. int retVal = GetEncoderClsid(L"image/bmp", &myClsId);
  3. int retVal = GetEncoderClsid(L"image/png", &myClsId);

Lo he modificado a JPG porque el BMP por defecto pesa 6 megas cada pantallazo



NORMAS DEL FORO - para garantizar el buen funcionamiento del Foro.
¿Te han ayudado? NO TE OLVIDES de darle a
¿Quieres dirigirte a alguien en tu post? Notifícale haciendo clic en su Nick
Kuk is offline   Responder Con Cita