x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How can I save a RenderTarget or a Texture to a file?

I've seen this question asked in a few different ways, but no one seems to be able to give a definitive answer.

To sum up, I'm using a SceneCaptureComponent2D to render to a RenderTarget. I want to save that RenderTarget to disk, at runtime. Everything with this is working fine, I just don't know how to do the last step of saving it to a file.

To be more explicit on what I'm trying to create, we have a literal camera in our game that the player can use to take pictures. We want to save these pictures to disk so that the player can share them.

A solution in Blueprint is preferred, but C++ is not a problem.

Product Version: UE 4.9
Tags:
more ▼

asked Nov 13 '15 at 06:10 PM in Rendering

avatar image

ddbrown30
238 11 23 104

avatar image Slavq Jan 10 '16 at 07:05 PM
  • I would also like to know this.

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

I wasn't able to find anything built in, so I ended up writing something myself. I'm relying on FHighResScreenshotConfig, as it already does most of the work. You could probably dig into it if you need more control.

The loop to set the alpha was a bit of a hack fix. For some reason, the alpha isn't set correctly on the render target, so you need to manually set it before saving.

 void SaveRenderTargetToDisk( UTextureRenderTarget2D* InRenderTarget, FString Filename )
 {
     FTextureRenderTargetResource* RTResource = InRenderTarget->GameThread_GetRenderTargetResource();
 
     FReadSurfaceDataFlags ReadPixelFlags( RCM_UNorm );
     ReadPixelFlags.SetLinearToGamma( true );
 
     TArray<FColor> OutBMP;
     RTResource->ReadPixels( OutBMP, ReadPixelFlags );
 
     for( FColor& color : OutBMP )
     {
         color.A = 255;
     }
 
 
     FIntRect SourceRect;
 
     FIntPoint DestSize( InRenderTarget->GetSurfaceWidth(), InRenderTarget->GetSurfaceHeight() );
 
 
     FString ResultPath;
     FHighResScreenshotConfig& HighResScreenshotConfig = GetHighResScreenshotConfig();
     HighResScreenshotConfig.SaveImage( Filename, OutBMP, DestSize, &ResultPath );
 }
more ▼

answered Jan 11 '16 at 03:00 PM

avatar image

ddbrown30
238 11 23 104

avatar image octafbr May 23 '17 at 04:03 PM

Very nice! This was the only thing I could find that got this working! It's really slow though, saving PNGs takes a lot of time. Do you have any idea how to improve that?

avatar image yanglong Aug 16 '17 at 06:20 AM

it seems not working if I use UTextureRenderTargetCube

avatar image Jack-Chen8 Oct 30 '17 at 02:46 AM

I have not FHighResScreenshotConfig class, Can you give to me? thank you very much.

avatar image Jack-Chen8 Oct 30 '17 at 05:54 AM

I have find the FHighResScreenshotConfig class, and very thanks your guide。 How about if use UTextureRenderTargetCube? expecting answer.

avatar image sleepdown Jun 06 '18 at 10:34 AM

have you find the answer?

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question