Hi,
Can someone help me with a boring problem?
I’m trying to generate a transparent .png based on a UI text present in a canvas (TextMeshPro beeing used to generate the text). For that, I’m currently using a code to capture the current frame of the scene Camera using the ReadPixels method. Then I save the resulted image to a png file using EncodeToPNG.
But those functions give 2 problems :
-
On windows the generated .png creates strange grey outlines or extra pixels on the text borders, between RGB channel and alpha channel. I don’t know where it’s coming from …
-
On Android the generated .png shows strange grey-glack abberations arround letters.
I looked for several ways to fix this (changing shader, texture format, color setting) but none worked. Does someone know why I have those strange results?
Thanks in advance.
TextureFormat : ARGB32
Text Shader : TextMeshPro/Distance Field
Android Build setting :
- Color space : Gamma
- Auto graphic API : enabled
- Color gamut : sRGB
Results :
Windows :
Android :
And below the code I’m using(sorry if I don’t post it correctly) :
//+-------+
//|Capture|
//+-------+
txtgen_camera.clearFlags = CameraClearFlags.Depth; //Remove Skybox and allows transparency sorting
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
txtgen_camera.targetTexture = rt;
//Texture parameters : Format and Resolution
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.ARGB32, true);
//Read pixels then release cam
txtgen_camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
txtgen_camera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
//Generate Image File
byte[] bytes;
bytes = screenShot.EncodeToPNG();
bytes = B83.Image.PNGTools.ChangePPI(bytes, 100, 100); //can be done in photoshop instead of Unity
Destroy(screenShot);
//+----------+
//|Image name|
//+----------+
string time = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
string name_file = "TestTxtGen" + "_" + time + extension;
//+----------+
//|Save image|
//-----------+
string local_path = Path.Combine(Application.persistentDataPath, "Texts");
string file_path = Path.Combine(local_path, name_file);
if (!System.IO.Directory.Exists(local_path))
{
Directory.CreateDirectory(local_path);
}
File.WriteAllBytes(file_path, bytes);


