0
votes

[Edited to provide additional info/partial answer at bottom]

Any constructive ideas on how to approach de-bugging this problem are appreciated.

I followed this tutorial on taking and displaying a screenshot using Unity's Screencapture.Capturescreenshot() method. https://www.youtube.com/watch?v=DQeylS0l4S4&t=92s

On display, my screenshot image (a basic cube in 3D space) appears translucent. Does anyone have any idea why and how I can fix it so it shows a true screen shot? I used another phone to take a picture of my actual screen when I'm taking the screen shot here (Sorry it's a little blurry; the cube is moving): Actual screen image

And a picture of the displayed screen shot. Notice how translucent it is: Screen capture image

This is code to capture the screen image:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TakeScreenshot : MonoBehaviour {
 
     public void TakeAShot()
     {
         StartCoroutine ("CaptureIt");
     }
 
     IEnumerator CaptureIt()
     {
         string timeStamp = System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss");
         string fileName = "Screenshot" + timeStamp + ".png";
         string pathToSave = fileName;
         ScreenCapture.CaptureScreenshot(pathToSave);
         yield return new WaitForEndOfFrame();
     }
 
 }

And here is the code for displaying. It converts the .png file to a texture, makes a sprite out of the texture, and displays the sprite on the canvas. My canvas is set to default parameters in the inspector.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System.IO;
 
 public class ScreenshotPreview : MonoBehaviour {
     
     public GameObject canvas;
     string[] files = null;
     int whichScreenShotIsShown= 0;
 
     // Use this for initialization
     void Start () {
         files = Directory.GetFiles(Application.persistentDataPath + "/", "*.png");
         if (files.Length > 0) {
             GetPictureAndShowIt ();
         }
     }
 
     void GetPictureAndShowIt()
     {
         string pathToFile = files [whichScreenShotIsShown];
         Texture2D texture = GetScreenshotImage (pathToFile);
         Sprite sp = Sprite.Create (texture, new Rect (0, 0, texture.width, texture.height),
             new Vector2 (0.5f, 0.5f));
         canvas.GetComponent<Image> ().sprite = sp;
     }
 
     Texture2D GetScreenshotImage(string filePath)
     {
         Texture2D texture = null;
         byte[] fileBytes;
         if (File.Exists (filePath)) {
             fileBytes = File.ReadAllBytes (filePath);
             texture = new Texture2D (2, 2, TextureFormat.RGB24, false);
             texture.LoadImage (fileBytes);
         }
         return texture;
     }
 }

Additional information:

So my thinking was that the problem had to either be in the capturing or the displaying. I realized I could look at the saved .png file in my Android files to see what they looked like before they are pulled back into the app for display.

The result? : The .png looks perfect. So it's just a problem with the displaying. I still haven't solved the whole problem but now I know the screen is being captured perfectly with Unity's Screencapture.Capturescreenshot method.

1

1 Answers

1
votes

It turns out that the 'alpha' value for my panel in the second scene was at about 50%, which in alpha values is about 127. To display the image exactly as it was taken on the screen, alpha needs to be at 255 , which represents 0% transparency. In general, many people want UI elements to have a degree of transparency so that you can see the main scene behind the UI elements. However, for viewing the screenshot I wanted no transparency. Therefore => Select the Panel in your hierarchy. Go to the Color quality in the Inspector. Double-click. Then set the Alpha value to 255.

If you have a situation where you do not have a dedicated scene just for viewing your screenshot and you want the alpha value to change within a single scene, you can change it using a script. In my case I could set my alpha to 255 permanently because the only purpose for that scene, and in fact the only purpose for that panel, was to display screenshots. Alpha value of Panel