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.

91967-shot2.png

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!

I have been struggling with this same exact issue all night. Just figured it out.

  1. Create a RenderTexture in the Editor
  2. Check the box [sRGB (Color RenderTexture)]

Fixed. :smiley:

Now use this render texture in your script and the color/gamma values should be good to go.