Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Best way to save a screenshot  (Read 8074 times)

0 Members and 1 Guest are viewing this topic.

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Best way to save a screenshot
« on: March 31, 2019, 07:12:59 pm »
Hi,

I'm trying to catch up with last SFML releases, and noticed that RenderWindow::capture is now deprecated:
Quote
Use a sf::Texture and its sf::Texture::update(const Window&) function and copy its contents into an sf::Image instead.

My use case is saving to a image file what is currently rendered on a render window.

Is the following code the best approach for SFML >= 2.4?
sf::Texture texture;
texture.create(render_window.getSize().x, render_window.getSize().y);
texture.update(render_window);
if (texture.copyToImage().saveToFile(filename))
{
    std::cout << "screenshot saved to " << filename << std::endl;
}
 

Also, this seems like quite a common task. Maybe it should be added in the wiki?
« Last Edit: March 31, 2019, 07:18:01 pm by Haze »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Best way to save a screenshot
« Reply #1 on: March 31, 2019, 09:36:02 pm »
Looks correct to me.

The Wiki is a community driven Wiki, would be great if you could add it! :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: Best way to save a screenshot
« Reply #2 on: April 01, 2019, 09:28:59 am »
Thanks for the confirmation.

I've added an entry to the Github wiki, under "Tutorials" section.

Nice to see all the core team is still active btw (I haven't used SFML since 2.1 release I think).

 

anything