- Home /
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);
}
Answer by TheFloatingSheep · Oct 11, 2015 at 06:55 PM
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 http://docs.unity3d.com/ScriptReference/TextureImporter.html http://docs.unity3d.com/ScriptReference/TextureImporter-npotScale.html
It's apparenrly called npotScale Just set it t "ToLarger" and it should be fine.
TextureImporter seems to be another class of object. I couldn't figure out how to incoperate npotScale. i am using objects of class Texture2D and RenderTexture and am not sure how they would interect with a TextureImporter class. I also am not doing any importing, only writing a file at the end.
i am using the ReadPixels with the texture2D and suppose to read from the RenderTexture which is suppose to be rendering to the target texture ins$$anonymous$$d of the screen at the moment. I feel the something is wrong with the rendering to texture and reading from it.
Your answer
Follow this Question
Related Questions
Rendering an HD PNG off-screen? 0 Answers
Get image of 3d model 0 Answers
Save rendertexture to image in file 2 Answers
Better faster solution for ReadPixels? 0 Answers