Screencapture.screenshot image is faint or translucent (Android)
Hello,
I followed this tutorial on taking a screenshot using Unity's Screencapture.Capturescreenshot() method. https://www.youtube.com/watch?v=DQeylS0l4S4&t=92s
It seemed pretty straight-forward and easy to follow. However, on display, the 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 moves):
... And then what it looks like on display here (Notice how you can see through it) ...:
Here is the code for the screen capture script:
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.
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;
}
}
It all works fine in the tutorial. I am using Android. Thank you in advance for your constructive ideas.
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.
Answer by Siggytron · Oct 06, 2020 at 03:26 PM
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 represent 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, you can change the alpha value using a script. In my case I could set my alpha to 255 permanently because the only purpose for that scene was to display screenshots.
Your answer
Follow this Question
Related Questions
Android: SVG Support 1 Answer
Capturing 360 Panorama in android 0 Answers
How does one prevent their Android Build from being transparent? 0 Answers
gameview capture in realtime 1 Answer