Create an image of the program window and save it in PNG format

I would like to click on the button to take a snapshot of the window (in the resolution in which the program window was opened) and save it to a folder so that you can use it in the future. Tell me how you can do this and that it is combined with SFML. If this is even possible in the version in which I described:)

Visual studio 2015 / c++ / sfml

#include <SFML\Graphics.hpp>
#include <iostream>
//#include <Windows.h>
//using namespace std;
using namespace sf;

struct Struct_SizeWindow {
int W = 1500, H = 1000, size = 1;
int w = size*W, h = size*H;
}; Struct_SizeWindow SizeWindow;

int main() {
RenderWindow window(VideoMode(SizeWindow.w, SizeWindow.h), "");
window.setPosition(Vector2i(500, 300));

int xPrimary = 0, yPrimary = 0, sizeShape = 4;

while (window.isOpen()) {

    Vector2i PosXY = Mouse::getPosition(window);
    Vector2f pos = window.mapPixelToCoords(PosXY);
    Vector2i pos2 = Mouse::getPosition(window);

    int x = pos.x / SizeWindow.size;
    int y = pos.y / SizeWindow.size;

    Vertex line[] = {
        Vertex(Vector2f(xPrimary, yPrimary)),
        Vertex(Vector2f(x, y))
    }; xPrimary = x; yPrimary = y;

    Event event;

    while (window.pollEvent(event)) {
        if (event.type == Event::Closed)
            window.close();
    }

    for (int i = 0; i < 2; i++) {
        window.draw(line, 2, sf::Lines);  window.display();
    }
}

}

 0
Author: Арчер, 2020-04-24