黑色截图

时间:2014-05-11 20:31:07

标签: c++ screenshot sdl-2

我只想从窗口保存图像,但问题是我保存的图像是黑色的。 它的尺寸合适,但只是黑色。

以下是代码:

IMG_SavePNG(SDL_GetWindowSurface(window), "C:/Users/Klaus/Desktop/hallo.png");

以下是窗口创建的代码:

    SDL_Window* window = SDL_CreateWindow("Famonex", 100, 100, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
    if (window == NULL) { cout << "Window couldn't be created." << endl; SDL_Delay(5000); quit = true; }

1 个答案:

答案 0 :(得分:-1)

我假设您已经初始化了一个 SDL_Renderer* pRenderer = SDL_CreateRenderer(window, -1, 0),执行了一些 SDL_RenderDraw... 操作并且 SDL_RenderPresent(pRenderer) 为您提供了您想要的 png 屏幕。

使用 SDL_Texture* 缓冲区有帮助吗?

    SDL_Surface* printSurface = SDL_GetWindowSurface(window);
    SDL_Texture* texture = SDL_CreateTextureFromSurface(pRenderer, printSurface);
    SDL_SetRenderTarget(pRenderer, texture);

    int width{ 0 }, height{0};
    SDL_QueryTexture(texture, NULL, NULL, &width, &height);
    SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
    SDL_RenderReadPixels(pRenderer, NULL, surface->format->format, surface->pixels, surface->pitch);
    
    IMG_SavePNG(surface, "vonKoch6.png");
    SDL_FreeSurface(surface);

与以下内容相同的想法:https://stackoverflow.com/a/51238719/10949765