Result of Viewport->ReadPixel color is 0

I make take screenshot. But it is only works on editor.

This is my Take Screenshot smaple code.

UGameViewportClient* GameViewport = GWorld->GetGameViewport();

FViewport* Viewport = GameViewport->Viewport;

Viewport->Draw();

FReadSurfaceDataFlags ReadPixelFlags(RCM_MinMaxNorm);
ReadPixelFlags.SetLinearToGamma(true);

// Read the contents of the viewport into an array.

TArray<FColor>  Bitmap;
if (Viewport->ReadPixels(Bitmap, ReadPixelFlags))
{
	check(Bitmap.Num() == Viewport->GetSizeXY().X * Viewport->GetSizeXY().Y);

	// Initialize alpha channel of bitmap
	for (auto& Pixel : Bitmap)
	{
		Pixel.A = 255;
	}
}
  
// Create screenshot folder if not already present.
if (IFileManager::Get().MakeDirectory(*FPaths::ScreenShotDir(), true))
{
	// Save the contents of the array to a bitmap file.
	FHighResScreenshotConfig& HighResScreenshotConfig = GetHighResScreenshotConfig();

	HighResScreenshotConfig.bDumpBufferVisualizationTargets = true;
	HighResScreenshotConfig.SetHDRCapture(true);

	FString ScreenshotSaveName;
	FFileHelper::GenerateNextBitmapFilename(_fileDirectory / _fileName, TEXT("png"), ScreenshotSaveName);
	HighResScreenshotConfig.SaveImage(ScreenshotSaveName, Bitmap, GameViewport->Viewport->GetSizeXY());
}

I got a good image from the editor, but not from Standalong. It only output black images. (Inserted alpha value.)

This is Result Image

What is problem?

I used the function that UE4 made. Like this

FScreenshotRequest::RequestScreenshot(_fileDirectory + TEXT("/") + _fileName + TEXT(".png"), bShowUI, bAddFilenameSuffix);

But i don’t know that why viewport pixle result is 0. I want someone tell me.

You can only call ReadPixels inside of the Draw function of Viewport class.

You can check the full comment out here: FViewPort::ReadPixels crash while play on "standalone Game" mode - #4 by Rama - C++ Programming - Unreal Engine Forums