Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Saved screenshot is too dark

Discussion in 'General Graphics' started by ttibi92, Sep 25, 2018.

  1. ttibi92

    ttibi92

    Joined:
    Nov 30, 2015
    Posts:
    4
    Hi,
    i'm trying to take a high resolution screenshot from script.
    The first image is what i'd like to save, but the second comes out...
    Untitled.png screen_1904x893_2018-09-25_13-54-26_0.png

    Any idea?
    The code taking and saving the screenshot:

    Code (CSharp):
    1. public void TakeHiResShot()
    2.     {
    3.         RenderTexture rt = new RenderTexture(resWidth, resHeight, 24)
    4.         {
    5.             enableRandomWrite = true
    6.         };  
    7.         Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
    8.         Camera.main.GetComponent<Camera>().targetTexture = rt;
    9.         Camera.main.GetComponent<Camera>().Render();
    10.         RenderTexture.active = rt;
    11.         screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    12.         screenShot.Apply();
    13.         Camera.main.GetComponent<Camera>().targetTexture = null;
    14.         RenderTexture.active = null;
    15.         Destroy(rt);      
    16.  
    17.         byte[] bytes = screenShot.EncodeToPNG();
    18.         string filename = ScreenShotName(resWidth, resHeight);
    19.         FileInfo info = new FileInfo(filename);
    20.         System.IO.File.WriteAllBytes(filename, bytes);
    21.  
    22.         ScreenCapturedEvent?.Invoke();
    23.     }
     
    Last edited: Sep 25, 2018
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,367
    Last edited: Sep 25, 2018
    Speedomon likes this.
  3. ttibi92

    ttibi92

    Joined:
    Nov 30, 2015
    Posts:
    4
    Tried both, same results. It seems like the encoding messes it up. Before that, the image appears correctly on the RenderTexture.
     
  4. ttibi92

    ttibi92

    Joined:
    Nov 30, 2015
    Posts:
    4
    Found the problem.
    The method works fine, if you call it from LateUpdate...

    Tried something like this, did not work, same results.

    IEnumerator TakeShot()
    {
    yield return new WaitForEndOfFrame();
    //taking the image...
    }
     
    halley likes this.
  5. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    I have the same problem, but only in editor mode. It works in all builds.