This worked in 4.20. It no longer works correctly in 4.22.
The issue is that when making a higher resolution image than the current display resolution the current display image is no longer being rendered at the requested resolution. Instead you get an image of the correct resolution but the image from the viewport is a small portion of the image, specifically it ends up in the upper left corner.
Any help would be greatly appreciated.
James McKenzie
DragonClaw Studios
Here is the code that I used:
FString UTWS_FunctionLibrary::TWS_ScreenShot(UObject* WorldContextObject, const FString& InFileName, bool bInShowUI, bool bUniqueName, bool bHighRes, int32 ResolutionX, int32 ResolutionY)
{
FString FileNameToUse = InFileName;
FString FileName, Path, Extention;
int counter = 0;
if (FileNameToUse.IsEmpty())
FileNameToUse = âscreenshot.pngâ;
if (FileNameToUse.Contains("âŚ")) return TEXT("");
FPaths::Split(FileNameToUse, Path, FileName, Extention);
Path = FPaths::ProjectSavedDir() + âScreenShotsâ + Path;
FileNameToUse = Path + â/â + FileName + â.â + Extention;
while(bUniqueName && FPaths::FileExists(FileNameToUse) && counter < 1000) {
FileNameToUse = Path + â/â + FileName + FString(std::to_string(++counter).c_str()) + â.â + Extention;
}
GIsHighResScreenshot = bHighRes;
if (bHighRes) {
GScreenshotResolutionX = (uint32)ResolutionX; GScreenshotResolutionY = (uint32)ResolutionY;
}
UGameViewportClient* ViewportClient = WorldContextObject->GetWorld()->GetGameViewport();
ViewportClient->OnScreenshotCaptured().Clear();
//canât use filePath as a function parameter, therefore need to âcaptureâ it (Capturing an object by name makes a lambda-local copy of the object.)
ViewportClient->OnScreenshotCaptured().AddLambda(
[FileNameToUse](int32 SizeX, int32 SizeY, const TArray<FColor>& Bitmap)
{
// Make sure that all alpha values are opaque.
TArray<FColor>& RefBitmap = const_cast<TArray<FColor>&>(Bitmap);
for (auto& Color : RefBitmap)
Color.A = 255;
TArray<uint8> CompressedBitmap;
FImageUtils::CompressImageArray(SizeX, SizeY, RefBitmap, CompressedBitmap);
//FString ScreenshotFilePath = GetScreenshotFilePath(CleanSlotFileName);
FFileHelper::SaveArrayToFile(CompressedBitmap, *FileNameToUse);
});
FScreenshotRequest::RequestScreenshot(InFileName, bInShowUI, bUniqueName);
if (bHighRes)
WorldContextObject->GetWorld()->GetGameViewport()->Viewport->TakeHighResScreenShot();
return FileNameToUse;
}