High Resolution ScreenShot Using targetTexture, ReadPixels and EncodeToPNG - Javascript

I am trying to get a screen shot with ReadPixels and enlarge the resolution for output. I will be taking a subsection (cropping it) before saving as a png to disk, so i can’t rely on the Application.CaptureScreenshot method.

The problem i am experiencing is the enlarged output is highly distorted Some portions are squished, while others are stretched. Attached are an unscaled and scaled example to show the distortion.

any help would be appreciated.

below is my function in javascript:

function screen_grab3(png_name:String, cam:Camera){
	yield WaitForEndOfFrame();
	//set render size and make texture
	var scale:float=4;
	var r_width:int=scale*Mathf.Floor(cam.pixelWidth);
	var r_height:int=scale*Mathf.Floor(cam.pixelHeight);
	var scaled_rt:RenderTexture=new RenderTexture(r_width, r_height, 24);
	
	//make 2d texture for writing to
	var screen_grab:Texture2D=new Texture2D(r_width, r_height, TextureFormat.RGB24, false);
	
	// set cam to render to the texture
	cam.targetTexture=scaled_rt;
	cam.Render();
	RenderTexture.active=scaled_rt;
	
	//write screenshot
	screen_grab.ReadPixels(new Rect(0, 0, r_width, r_height), 0, 0);
	screen_grab.Apply();
	
	//revert cam settings
    cam.targetTexture=null;
    RenderTexture.active=null;
    
    //convert to png
	var bytes=screen_grab.EncodeToPNG();
    
    //destroy stuff
	Destroy (screen_grab);
    //Destroy(scaled_rt);
    
	//save to file
	var path:String=Application.persistentDataPath+"/"+png_name+".png";
	File.WriteAllBytes(path, bytes);
}

I was working on an app and i had 2 textures with the type set on Advanced, Format: RGB Compressed PVRTC4.
It was the default texture, and the script i made was taking it from an UI.Image.
Then i upload it to a server, and that canvas isn’t visible anyway at that moment.
But when i enable that canvas… i download another texture from the server for that UI.Image.

The idea is that… the texture that i manually assigned to the UI.Image, looked exactly like your
(report01-strech-scale.png).
I selected the texture in the Project window and i changed the “Non Power of 2” propriety to “ToLarger”.
Your texture, is already set on “Advanced” when you make it in the script because otherwise it wouldn’t be readable, so…
I found here the propriety Unity - Scripting API: TextureImporter
Unity - Scripting API: TextureImporter.npotScale

It’s apparenrly called npotScale Just set it t “ToLarger” and it should be fine.