Captura de pantalla en C

0x666

Se le pregunte a un amigo h4x0r que tengo y me ayudo, pego el source.

Ahora estoy peleando en comprimir la imagen, a jpg o usar zlib y usar una compresion general.

/*
CAPTURA PANTALLA
Programado por XpyXt
web: http://xpyxt.no-ip.com
mail: xpyxt@xpyxt.no-ip.org

Licencia de implementacion: publico este source bajo GPL, es decir, si tu
usas este codigo fuente en algun programa deberas publicar su source. Como
me entere de que lo has usado y no has publicado el codigo fuente voy a tu
casa y te amputo las manos. ¿Entendido?

Explicacion y modo de uso: Este source nos captura la pantalla del windows,
Hay 3 arrays de datos, 2 cabeceras del BMP y el RAW del bmp.

array CBA1 &bfh, Tamaño de la CBA1 sizeof(bfh)
array CBA2 &bmi.bmiHeader, Tamaño de la CBA2 sizeof(BITMAPINFOHEADER)
array (char *)pbBits, Tamaño del RAW bmi.bmiHeader.biSizeImage

*/

//#define WINVER 0x501
//#define WINVER 0x400

#include <windows.h>
#include <stdio.h>
#include <wingdi.h>

void cap();
int main()
{
cap();
return 0;
}

void cap()
{

BITMAPINFO bmi;
BITMAPFILEHEADER bfh;


int     nWidth;
int     nHeight;

HWND    hWnd;
HDC     hdc ;
HDC     memDC;
HBITMAP hbm ;
HBITMAP hbmOld;
BYTE *pbBits;


HANDLE hfile;
DWORD dwWritten;

nWidth  = GetSystemMetrics(SM_CXSCREEN);
nHeight = GetSystemMetrics(SM_CYSCREEN);

hWnd    = GetDesktopWindow();
hdc     = GetDC(hWnd);
memDC   = CreateCompatibleDC(hdc);
hbm     = CreateCompatibleBitmap(hdc, nWidth, nHeight);
hbmOld  = (HBITMAP)SelectObject(memDC, hbm);

BitBlt(memDC, 0, 0, nWidth, nHeight, hdc, 0, 0, SRCCOPY);
                             


ZeroMemory(&bmi, sizeof(bmi));

bmi.bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth        = nWidth;
bmi.bmiHeader.biHeight       = nHeight;
bmi.bmiHeader.biBitCount     = 24;
bmi.bmiHeader.biPlanes       = 1;
bmi.bmiHeader.biCompression  = BI_RGB;
bmi.bmiHeader.biSizeImage    = 32 * nWidth * nHeight / 8;


pbBits =  (byte *) malloc(bmi.bmiHeader.biSizeImage); ;



GetDIBits( memDC, 
			 hbm,
			 0,
			 bmi.bmiHeader.biHeight,
			 pbBits,
			 &bmi,
			 DIB_RGB_COLORS );





bfh.bfType      = ('M' << 8) + 'B';
bfh.bfSize      = sizeof(BITMAPFILEHEADER)  +
				  bmi.bmiHeader.biSizeImage +
				  sizeof(BITMAPINFOHEADER); 
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);



//(char *)pbBits // string de datos imagen sin cabezeras



// por si lo quieres guadar en el fichero. 
hfile = CreateFile( ("c:\\screenXXX.bmp"),
						   GENERIC_WRITE,
						   0,
						   0,
						   OPEN_ALWAYS,
						   0,
						   0 ); 


 
WriteFile(hfile,&bfh,           sizeof(bfh),               &dwWritten, NULL); 
WriteFile(hfile,&bmi.bmiHeader, sizeof(BITMAPINFOHEADER),  &dwWritten, NULL); 
WriteFile(hfile,pbBits,         bmi.bmiHeader.biSizeImage, &dwWritten, NULL); 


CloseHandle(hfile);


SelectObject(memDC, hbmOld);
DeleteDC(memDC);
ReleaseDC(hWnd,hdc); 
DeleteObject(hbm);

free(pbBits);

}

LOc0

Interesante, aunque la captura sea en BMP. Gracias por compartilo.

Salu2 ;)

aLeX

En momentos así no veo con tan malos ojos a Java y su clase Robot...

javithelong

Estoy de acuerdo con alex, bastante mas complicado en C, pero vamos, esta bien saberlo...

cabron

Hace ya bastante que C dejó de ser el lenguaje estándar para programar Windows, y fue sustituido por C++ con MFC.

El motivo es evidente xD

Usuarios habituales

  • cabron
  • javithelong
  • aLeX
  • LOc0
  • 0x666