# HOW TO...Get screenshot/texture from 2d Scene Capture Comp at runtime - C++

**URL:** <https://forums.unrealengine.com/t/how-to-get-screenshot-texture-from-2d-scene-capture-comp-at-runtime-c/1618727>\
**Category:** C++\
**Tags:** Scene-Capture, unreal-engine, UE5, texture-channel\
**Created:** [January 16, 2024, 9:12am UTC](https://forums.unrealengine.com/t/how-to-get-screenshot-texture-from-2d-scene-capture-comp-at-runtime-c/1618727 "2024-01-16T09:12:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![elguapador](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@elguapador](https://forums.unrealengine.com/u/elguapador)\
**Post date:** [January 16, 2024, 9:12am UTC](https://forums.unrealengine.com/t/how-to-get-screenshot-texture-from-2d-scene-capture-comp-at-runtime-c/1618727/1 "2024-01-16T09:12:32Z")

</div>

It took me a while but here is some code to get a still image as a texture at runtime (as of UE5.03). Note that the async lambda currently does nothing. I will update soon with a more efficient multi-threaded division of ;abour. Consider trade-offs when using:

```auto
	AsyncTask(ENamedThreads::GameThread, [this, Camera]()
		{
                        // "Camera" = 2D Scene Capture Component
			if (Camera)
			{
                                // define image resolution
				Camera->TextureTarget = UCanvasRenderTarget2D::CreateCanvasRenderTarget2D(this, UCanvasRenderTarget2D::StaticClass(), 512, 512);
				Camera->CaptureScene();
				// Creates Texture2D to store TextureRenderTarget content
				UTexture2D* Texture = UTexture2D::CreateTransient(Camera->TextureTarget->SizeX, Camera->TextureTarget->SizeY);
#if WITH_EDITORONLY_DATA
				Texture->MipGenSettings = TMGS_NoMipmaps;
#endif
				Texture->SRGB = false;
				Texture->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;

				// Read the pixels from the RenderTarget and store them in a FColor array
				TArray<FColor> SurfData;
				FRenderTarget* RenderTarget = Camera->TextureTarget->GameThread_GetRenderTargetResource();
				RenderTarget->ReadPixels(SurfData);
				// some alpha channels are set to 0 by default, make sure all pixels opaque
				for (auto& Pixel : SurfData)
				{
					Pixel.A = 255.f;
				}

				// Lock and copies the data between the textures
				void* TextureData = Texture->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
				const int32 TextureDataSize = SurfData.Num() * 4;
				FMemory::Memcpy(TextureData, SurfData.GetData(), TextureDataSize);
				Texture->GetPlatformData()->Mips[0].BulkData.Unlock();
				// Apply Texture changes to GPU memory
				Texture->UpdateResource();
				// Do something with texture
			}
		});

```

Hope this saves someone a few hours of research 🙂
