0

I'm trying to use SDL2 to take a screenshot of the entire desktop in windows. However, when looking at the resulting .bmp file it's completely black. Any help would be appreciated.

Here's my code:

SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window* window = SDL_CreateWindowFrom(GetDesktopWindow());

    int w, h;

    SDL_GetWindowSize(window, &w, &h);

    uint32_t wnd_pix_fmt = SDL_GetWindowPixelFormat(window);

    if(wnd_pix_fmt == SDL_PIXELFORMAT_UNKNOWN)
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Window pix fmt error", SDL_GetError(), NULL);

    SDL_Surface* screenshot = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, wnd_pix_fmt);
    if(!screenshot)
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "RGB surface error", SDL_GetError(), NULL);

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
    if(!renderer)
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Renderer error", SDL_GetError(), NULL);

    if(SDL_RenderReadPixels(renderer, &screenshot->clip_rect, screenshot->format, screenshot->pixels, screenshot->pitch))
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "RendererReadPixels error", SDL_GetError(), NULL);

    SDL_SaveBMP(screenshot, "screenshot.bmp");

    SDL_FreeSurface(screenshot);

NOTE: Even when I set SDL_Window* window to a window created with SDL_CreateWindow it's still completely black. On a different forum, they mentioned this may have to do with a double buffering issue. I'm unaware of how to solve such an issue though.

  • 2
    RenderReadPixels reads data you previously drawn via other SDL renderer functions, or sometimes with underlying graphics API (d3d/opengl/vulkan) that SDL renderer uses; it does not read other data, especially that belongs to different application. Since you already use OS specific functions, you have better chances in going all the way down to e.g. https://stackoverflow.com/questions/3291167/how-can-i-take-a-screenshot-in-a-windows-application – keltar Sep 12 '18 at 08:22
  • 1
    what on earth did you expect to happen here? you never render anything. – Brad Allred Sep 12 '18 at 13:23
  • @BradAllred I don't know, might have to do with a question on this very site specifically using `RenderReadPixels` in a [screenshot example](https://stackoverflow.com/questions/22315980/sdl2-c-taking-a-screenshot?noredirect=1&lq=1). – Edward Severinsen Sep 12 '18 at 20:08
  • Also, there being nothing on [this](https://wiki.libsdl.org/SDL_RenderReadPixels) documentation page of `RenderReadPixels` alluding to it only being useable in that case. – Edward Severinsen Sep 12 '18 at 20:11

1 Answers1

2

You have a fundamental misconception of what SDL_RenderReadPixels() does. Indeed it is used to take "screenshots", but the "screenshot" will be of what you have rendered using that specific renderer, nothing else. You will not be able to accomplish what you want using anything available in SDL.

Taking a screenshot of the entire screen typically requires elevated permissions (I have no idea about Windows) and is outside the scope of SDL.

Brad Allred
  • 7,323
  • 1
  • 30
  • 49
  • Yes, I need png support anyways so it looks like I'm going to have to dive into ffmpeg again. And use the winapi as well. Thank you for your time. – Edward Severinsen Sep 12 '18 at 21:08