Hi all! It seems that some of the screenshot functionality isn’t working entirely as intended in the normal setup right now, particularly with regards to saving screenshots to a specific directory/filename. Therefore, I had to do a fair amount of research, came across a bunch of different things that didn’t exactly work right, and came up with the following, which I thought might serve to help both people who want to take screenshots, and people who want to save texture data, and neither of those operations seem to be 100% obvious.
To save bitmap data using this, you’ll need the X and Y dimensions of the bitmap data, a TArray<FColor> of the bitmap data, and the FString path you want to store it in.
// i threw this into a static utility class, because I have a feeling it's going to be very useful in the future
bool UUtils::SaveBitmapAsPNG(int32 sizeX, int32 sizeY, const TArray<FColor>& bitmapData, const FString& filePath) {
TArray<uint8> compressedBitmap;
FImageUtils::CompressImageArray(sizeX, sizeY, bitmapData, compressedBitmap);
return FFileHelper::SaveArrayToFile(compressedBitmap, *filePath);
}
Then, in the class where you are wanting to handle your screenshotting, you’ll need to make two functions, one that requests the screenshot using FScreenshotRequest, and one that receives the screenshot data in the next frame, when the screenshot is actually completed.
static FString MyPlayerController::cachedScreenShotLocation = "";
void MyPlayerController::GetShot(FString path) {
cachedScreenShotLocation = path;
UGameViewportClient* GameViewportClient = GEngine->GameViewport;
// register notification for when the next screenshot is completed
GameViewportClient->OnScreenshotCaptured().AddUObject(this, &MyPlayerController::OnScreenShotCaptured);
FScreenshotRequest::RequestScreenshot(path, false, false);
}
void MyPlayerController::OnScreenShotCaptured(int32 InSizeX, int32 InSizeY, const TArray<FColor>& InImageData) {
// remove notification for when shot is completed, we don't care if someone else requested a screenshot, just
// the most recent request that came in involving this object.
GEngine->GameViewport->OnScreenshotCaptured().RemoveAll(this);
if (!UIIDUtils::SaveBitmapAsPNG(InSizeX, InSizeY, InImageData, cachedScreenShotLocation)) {
UE_LOG(LogTemp, Error, TEXT("Error saving screenshot to %s"), *cachedScreenShotLocation);
} else {
UE_LOG(LogTemp, Warning, TEXT("screenshot saved to %s"), *cachedScreenShotLocation);
}
}