Print Back buffer as PNG

FDelegateHandle s_delegateHandle_OnBackBufferReadyToPresent_renderThread;
.
.
.
if (FSlateApplication::IsInitialized())
{
	auto &slateApplication = FSlateApplication::Get();
	auto *const slateRenderer = slateApplication.GetRenderer();
	check(slateRenderer != nullptr);
	// OnBackBufferReadyToPresent_renderThread
	{
		check(!s_delegateHandle_OnBackBufferReadyToPresent_renderThread.IsValid());
		s_delegateHandle_OnBackBufferReadyToPresent_renderThread = slateRenderer->OnBackBufferReadyToPresent().AddStatic(OnBackBufferReadyToPresent_renderThread);
		check(s_delegateHandle_OnBackBufferReadyToPresent_renderThread.IsValid());
	}

.
.
.
void OnBackBufferReadyToPresent_renderThread(SWindow& io_window, const FTexture2DRHIRef& i_backBuffer)
{
	// Save the back buffer as a PNG
	SaveBackBufferAsPNG(i_backBuffer);
}

void SaveBackBufferAsPNG(const FTexture2DRHIRef& BackBuffer)
{
	// Read the pixels from the back buffer
	TArray<FColor> Bitmap;
	FIntRect Rect(0, 0, BackBuffer->GetSizeX(), BackBuffer->GetSizeY());
	FRHICommandListImmediate& RHICmdList = GRHICommandList.GetImmediateCommandList();
	RHICmdList.ReadSurfaceData(BackBuffer, Rect, Bitmap, FReadSurfaceDataFlags());

	// Save the pixels as a PNG file
	IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
	TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);

	if (ImageWrapper.IsValid() && ImageWrapper->SetRaw(Bitmap.GetData(), Bitmap.GetAllocatedSize(), BackBuffer->GetSizeX(), BackBuffer->GetSizeY(), ERGBFormat::BGRA, 8))
	{
		const TArray64<uint8>& PNGData = ImageWrapper->GetCompressed(100);

		// Generate a timestamp
		FString Timestamp = FDateTime::Now().ToString(TEXT("%Y%m%d_%H%M%S"));

		// Generate a unique filename with the timestamp
		FString Filename = FString::Printf(TEXT("CapturedBackBuffer_%s.png"), *Timestamp);

		// Save the file
		FFileHelper::SaveArrayToFile(PNGData, *FPaths::Combine(FPaths::ProjectSavedDir(), Filename));
	}
}

Is this the right way to access the back buffer, because when I run the above code in editor the png that I get is of the entire editor window, i was expecting it to be of just what the camera is seeing.

welcome to the forums.
in cpp there’s the functionality to save a screenshot that will save the viewport. you can even do it in high resolution.
that might be more help. you can save as png or exr or get a byte array.
though i’m not sure if that will serve the same purpose as a back buffer.

otherwise you might wanna take a look at sceneview
in this link there’s mention to the backbuffer.