- Home /
EndcodeToPNG resulting in dark image
I'm working on a project that involves players taking screenshots with an in-game camera.
After some digging, the approach I decided to go with involves using a render texture, reading it's pixels and saving the result as a PNG. The issue I'm having is that the images all end up darker or more washed out than what I see when I run the project.
My first guess was that it had to do with the color format, but both the render texture and texture format for the Texture2D are ARGB32.
public IEnumerator SaveCameraView()
{
yield return new WaitForEndOfFrame();
RenderTexture rendText= RenderTexture.active;
RenderTexture.active = screenShotCam.targetTexture;
screenShotCam.Render();
Texture2D cameraImage= new Texture2D(screenShotCam.targetTexture.width, screenShotCam.targetTexture.height, TextureFormat.ARGB32, false);
cameraImage.ReadPixels(new Rect(0, 0, screenShotCam.targetTexture.width, screenShotCam.targetTexture.height), 0, 0);
cameraImage.Apply();
RenderTexture.active = rendText;
// store the texture into a .PNG file
byte[] bytes = cameraImage.EncodeToPNG();
imageNum++;
print (Application.dataPath);
File.WriteAllBytes(Application.dataPath + "/Screenshots/shot"+imageNum+".png" , bytes);
}
I'm thinking it might have something to do with the alpha channel, since the images look quite different depending on what I use to open them, but I'm not sure how to proceed.
Any help would be hugely appreciated. Thanks!
The screen does have it's own light (via a culling mask) with an intensity of 1 that might account for some of the difference, so I figure it's worth noting, but in any case, I'm wondering how to essentially capture what I see on that screen.
It's probably related to HDR, which I don't think gets encoded in the rendertextures. If you disable HDR on your camera and project, does that fix the problem?
Unfortunately no. I did have Tonemapping on the camera as a crude way of adjusting exposure, but even when I disable that and the HDR, I still get inconsistent images. Though now they are more contrasty.
The Tonemapping exposure adjustment seemed to be washing things out, so losing that at least resolved that issue, though I'll have to come up with something else to allow exposure adjustments.
Looks very much like a gamma/sRGB vs linear issue. Check `RenderTextureReadWrite`.
Answer by SpaceManDan · May 24, 2017 at 07:46 AM
I have been struggling with this same exact issue all night. Just figured it out.
Create a RenderTexture in the Editor
Check the box [sRGB (Color RenderTexture)]
Fixed. :D
Now use this render texture in your script and the color/gamma values should be good to go.
Your answer
![](https://koobas.hobune.stream/wayback/20220612115740im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to save a RenderTexture to a png from a different camera? 1 Answer
Save rendertexture to image in file 2 Answers
Wierd Screen Shot Problem, texture of screen shot looks different in editor and in system. 1 Answer
RenderTexture with image effects baked in 2 Answers
dynamic hole in layer / texture / camera 0 Answers