And this is how you basically do it:
UGameViewportClient* ViewportClient = TheWorld->GetGameViewport();
ViewportClient->OnScreenshotCaptured().Clear();
ViewportClient->OnScreenshotCaptured().AddLambda(
[ScreenshotFilePath](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, *ScreenshotFilePath);
});