Sign in to follow this  
johnysums

copy screen to sdl_surface

Recommended Posts

Hello, im trying to figure out how you can copy the screen of a separate window open in windows so that i may do something with it. is there any way to copy the screen from a window of another program onto an sdl_surface?? if not, there must at least be a way to print_screen (as when i press the keyboard button) onto an sdl_surface, right??

Share this post


Link to post
Share on other sites
The GDI code for creating a HBITMAP containing a screenshot of the desktop looks like this (error checking and resource deallocation code not shown):

HDC hdc = GetDC(NULL); // get the desktop device context
HDC hDest = CreateCompatibleDC(hdc); // create a device context to use yourself

// get the height and width of the screen
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);

// create a bitmap
HBITMAP hbDesktop = CreateCompatibleBitmap( hdc, width, height);

// use the previously created device context with the bitmap
SelectObject(hDest, hbDesktop);

// copy from the desktop device context to the bitmap device context
BitBlt( hDest, 0,0, width, height, hdc, 0, 0, SRCCOPY);

Once you've got the info in an HBITMAP you've got a couple of options. The more efficient one is to use the HBITMAP to create an SDL_Surface. Do a search for "HBITMAP to SDL_Surface" to handle that. Or you can write the HBITMAP to a file and then use SDL to load the file. This is highly inefficient but generally requires less code to do if you use the right libraries. Do a search for "HBITMAP to file" or "HBITMAP to BMP" or whatever image format you want to use.

Share this post


Link to post
Share on other sites
thanks, thats just what i was looking for. But i guess then there's no way to copy the screen of a separate window (and not just a print_screen of the whole display)? what im trying to do is tell which chess pieces moved in this separate program by taking an image of the program's window and checking the squares. print_screen would work if i keep my prog minimized but it would help if i could my program open on top of the other program and still be able to copy the screen of the window. my guess is there's some restriction on just copying any window open, correct??

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this