views:

116

answers:

2

Hi
How can I capture screen and save it as am image in C?
OS: windows (XP & Seven)

Thanks

+3  A: 

Have you tried google? This forum entry has an example, complete with C source code using the Win32 API.

EDIT: Found a duplicate in the meantime: http://stackoverflow.com/questions/997175/how-can-i-take-a-screenshot-and-save-it-as-jpeg-on-windows

Greg S
+1  A: 

in case you don't want to bother to click on link

#include <windows.h>

bool SaveBMPFile(char *filename, HBITMAP bitmap, HDC bitmapDC, int width, int height);

bool ScreenCapture(int x, int y, int width, int height, char *filename){
// get a DC compat. w/ the screen
HDC hDc = CreateCompatibleDC(0);

// make a bmp in memory to store the capture in
HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), width, height);

// join em up
SelectObject(hDc, hBmp);

// copy from the screen to my bitmap
BitBlt(hDc, 0, 0, width, height, GetDC(0), x, y, SRCCOPY);

// save my bitmap
bool ret = SaveBMPFile(filename, hBmp, hDc, width, height);

// free the bitmap memory
DeleteObject(hBmp);

return ret;
}

main(){
ScreenCapture(500, 200, 300, 300, "c:\\testScreenCap.bmp");
system("pause");
}
mihirpmehta
this code didn't work for me.
Victor
did you get error message...? did you get null value... ?
mihirpmehta
Snigger