Questa libreria permette la cattura della schermata corrente (Desktop window) ed il suo salvataggio in formato bitmap e jpeg.
Per la conversione in formato jpg viene utilizzata la libreria CxImage (versione 5.71).
Codice e riferimenti:
Cattura finestra: HBITMAP CaptureScreen(void) { HWND hDesktopWnd; HDC hDesktopDC,hCaptureDC; HBITMAP hCaptureBitmap; // Ricavo la risoluzione dello schermo int nScreenWidth = GetSystemMetrics(SM_CXSCREEN); int nScreenHeight = GetSystemMetrics(SM_CYSCREEN); // Aquisizione handle finestra Desktop hDesktopWnd = GetDesktopWindow(); // Ricavo il DC della finestra hDesktopDC = GetDC(hDesktopWnd); // Creo un DC in memoria compatibile con la finestra catturata hCaptureDC = CreateCompatibleDC(hDesktopDC); // Creo una bitmap compatibile associata al DC della finestra catturata hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight); // Seleziono l'immagine dal DC SelectObject(hCaptureDC,hCaptureBitmap); // Trasferisco il contenuto del DC catturato nel DC comptabile creato BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDesktopDC,0,0,SRCCOPY); // Rilascio il DC della finestra ReleaseDC(hDesktopWnd,hDesktopDC); // Cancello il DC compatibile DeleteDC(hCaptureDC); // Ritorno l'handle della bitmap return hCaptureBitmap; }
|
Salvataggio bitmap: BOOL SaveCapturedBitmap(char* FilePath,HBITMAP hBitmap) { HDC hdc = NULL; FILE* fp = NULL; LPVOID pBuf = NULL; BITMAPINFO bmpInfo; BITMAPFILEHEADER bmpFileHeader; hdc=GetDC(NULL); ZeroMemory(&bmpInfo,sizeof(BITMAPINFO)); bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); GetDIBits(hdc,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS); if (bmpInfo.bmiHeader.biSizeImage<=0) bmpInfo.bmiHeader.biSizeImage = bmpInfo.bmiHeader.biWidth* abs(bmpInfo.bmiHeader.biHeight)* (bmpInfo.bmiHeader.biBitCount+7)/8; if ((pBuf = malloc(bmpInfo.bmiHeader.biSizeImage))==NULL) { //printf("Unable to Allocate Bitmap Memory"); return false; } bmpInfo.bmiHeader.biCompression=BI_RGB; GetDIBits(hdc, hBitmap, 0, bmpInfo.bmiHeader.biHeight, pBuf, &bmpInfo, DIB_RGB_COLORS); if((fp = fopen(FilePath,"wb"))==NULL) { //printf("Unable to Create Bitmap File"); return false; } bmpFileHeader.bfReserved1=0; bmpFileHeader.bfReserved2=0; bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+ sizeof(BITMAPINFOHEADER)+ bmpInfo.bmiHeader.biSizeImage; bmpFileHeader.bfType='MB'; bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+ sizeof(BITMAPINFOHEADER); fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp); fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp); fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp); fclose(fp); return true; }
|
Salvataggio jpeg: BOOL SaveCapturedJpeg(char* FilePath,HBITMAP hBitmap) { CxImage image; image.CreateFromHBITMAP(hBitmap); if (image.IsValid()) image.Save(FilePath,CXIMAGE_FORMAT_JPG); else return false; return true; }
|
#pragma comment(linker,"/subsystem:windows") #pragma comment(linker,"/NODEFAULTLIB:LIBC.LIB") #include "Screen.h" int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow) { HBITMAP hCaptureBitmap; // Catturo la schermata hCaptureBitmap = CaptureScreen(); // Ne salvo una copia in formato bitmap SaveCapturedBitmap("c:\screen.bmp",hCaptureBitmap); // Ne salvo una copia in formato jpeg SaveCapturedJpeg("c:\screen.jpg",hCaptureBitmap); // Rilascio DeleteObject(hCaptureBitmap); return 0; }
|
Nota: Viene nascosta la finestra del programma per evitarne la cattura.
 | | Downloads: 288 | | | |  | | Downloads: 263 | | | |  | | Downloads: 155 | | | |
|