Jump to content

Screenshot without the GUI?


photo

Recommended Posts

Hi, I'm trying to do a screenshot without the GUI visible and in high res.

Using "screenshot" command would capture the GUI, and will also use the current image size.

I'm using this code at the moment to capture the current view and save it in the user's Picture folder:

#include <chrono>  // chrono::system_clock
#include <ctime>   // localtime
#include <sstream> // stringstream
#include <iomanip> // put_time
#include <string>  // string
#include <iostream>
#include <windows.h>
#include <shlobj.h>
  
std::string GetImagesFolderPath()
{
	PWSTR path;
	HRESULT result = SHGetKnownFolderPath(FOLDERID_Pictures, 0, NULL, &path);
	if (SUCCEEDED(result))
	{
		std::wstring wstr(path);
		CoTaskMemFree(path);
		std::string str(wstr.begin(), wstr.end());
		return str;
	}
	else return "";
}
void AppSystemLogic::cmd_printscreen(int argc, char** argv)
{
	auto now = std::chrono::system_clock::now();
	auto in_time_t = std::chrono::system_clock::to_time_t(now);

	std::stringstream ss;
	ss << GetImagesFolderPath() << "\\" << std::put_time(std::localtime(&in_time_t), "capture_%Y%m%d_%H%M%S.png");

	const int viewportHeight = 1080;
	const int viewportWidth = 1920;

	ViewportPtr viewport = Viewport::create();
	viewport->setSkipFlags(Viewport::SKIP_VELOCITY_BUFFER | Viewport::SKIP_VISUALIZER);
	viewport->setRenderMode(Viewport::RENDER_DEPTH_GBUFFER_FINAL);
	viewport->setNodeLightUsage(Viewport::USAGE_WORLD_LIGHT);

	TexturePtr texture = Texture::create();
	texture->create2D(viewportWidth, viewportHeight,
		Unigine::Texture::FORMAT_RGB8, // RGB only otherwise the saved PNG will use the alpha (sky is masked out)
		Texture::SAMPLER_FILTER_LINEAR | Texture::SAMPLER_ANISOTROPY_16 | Texture::FORMAT_USAGE_RENDER);

	{
		const int streaming_mode = Render::getStreamingMode();
		Render::setStreamingMode(Render::STREAMING_FORCE); // force load texture
		const float e = Render::getExposureAdaptation();
		Render::setExposureAdaptation(0);
		viewport->renderTexture2D(Game::getPlayer()->getCamera(), texture);
		Render::setExposureAdaptation(e);
		Render::setStreamingMode(streaming_mode); // restore streaming mode
	}

	ImagePtr image = Image::create();
	texture->getImage(image);

	const char* filename = ss.str().c_str();
	image->save(filename); // JPG doesn't work. Why?
	Log::message("Screenshot saved to %s\n", filename);
}

But the saved image is very noisy, especially all gradients and shadows. Is there anything I could do?

With my code:

capture_20230622_154521.thumb.png.d1e83256b23c03e88b48c22720174798.png

 

with screenshot command:

with_screenshot_command.thumb.png.6b808fd8e1c8fbdc177e4f7fe6aba9c5.png

Link to comment
×
×
  • Create New...