# copy screen to sdl_surface

## Recommended Posts

johnysums    100
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 on other sites
johnysums    100
sorry for bumping up my thread, but i really need to know the answer. Ive been doing some searching but i wasnt able to find anything, even in the Alternative Game Lib. section. any ideads ?

##### Share on other sites
SiCrane    11839
Do you mind using platform dependent code?

##### Share on other sites
johnysums    100
im only writing this for myself, so no i dont care.

##### Share on other sites
SiCrane    11839
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 on other sites
johnysums    100
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 on other sites
clashie    632
Just use the HDC of the window you want instead of 0.

Also you could use a DIB Section instead and it will give you access to all the pixel data of what you just copied.