C++ screenshot GetViewportScreenShot returning all black pixels

I have the following problem that I cannot find an answer to online.
I am making a screenshot, I need it to actually be low res (for networking reasons, not important here) and when doing it in Editor World everything works fine, but when using it in game mode, everything goes through, does not throw any errors, but the image is black.
After calling GetViewportScreenShot() on the correct (i guess) viewport every pixel is RBG black.
I dont need actual file on disk, I just need the binary image so I’m doing it like this.

After a few iterations, i separated the Editor mode and game mode functions, and the game mode looks like this, does anyone know what the problem could be, and how to take a screenshot (in non high res mode) in Game World?
This code is where I’m currently now so its not perfect but it creates the image that is black.and tells the story :slight_smile:
Thanks!

This code produces a perfect screenshot that i need, in editor mode:(GetImageWrapper is just initing of the ImageWrapper module for JPEG)

FViewport* Viewport = GEditor->GetActiveViewport();
double StartScreenshotTime = FPlatformTime::Seconds();
TArray<FColor> ViewportPixels;
FIntRect ViewRect(0, 0, Viewport->GetSizeXY().X, Viewport->GetSizeXY().Y);

bool bDidGetScreenShot = GetViewportScreenShot(Viewport, ViewportPixels, ViewRect);

GetImageWrapper()->SetRaw(&ViewportPixels[0], ViewportPixels.Num() * sizeof(FColor), Viewport->GetSizeXY().X, Viewport->GetSizeXY().Y, ERGBFormat::BGRA, 8);
const TArray64<uint8> CompressedImage = GetImageWrapper()->GetCompressed(Quality);

return CompressedImage;

This code produces a black image

UGameViewportClient* GameViewport = GEngine->GameViewport;

FVector2D ViewportSize;
GameViewport->GetViewportSize(ViewportSize);
FIntRect Rect(0, 0, ViewportSize.X, ViewportSize.Y);

FViewport* Viewport = GameViewport->Viewport;
TArray<FColor> ViewportPixels;
GetViewportScreenShot(Viewport, ViewportPixels, Rect);

GetImageWrapper()->SetRaw(&Viewport[0], ViewportPixels.Num() * sizeof(FColor), Viewport->GetSizeXY().X, Viewport->GetSizeXY().Y, ERGBFormat::RGBA, 8);
const TArray64<uint8> CompressedImage = GetImageWrapper()->GetCompressed(Quality);
return CompressedImage;

For future travelers

I managed to get this working with a totally different method, not sure if it’s the best method, but it works!

TArray64<uint8> MyClass::GetScreenShotInGame(int Quality)
{
    TArray<FColor> ViewportPixels;
    FIntVector OutSize;
    FSlateApplication::Get().TakeScreenshot( GEngine->GameViewport->GetWindow().ToSharedRef(), ViewportPixels, OutSize);

    GetImageWrapper()->SetRaw(&ViewportPixels[0], ViewportPixels.Num() * sizeof(FColor), OutSize.X, OutSize.Y, ERGBFormat::BGRA, 8);
    const TArray64<uint8> CompressedImage = GetImageWrapper()->GetCompressed(Quality);
    return CompressedImage;
}