EncodeToPng and WebCamTexture PROBLEM

Hello everyone I have a problem. My code saves or encodes Png photo very badly. It saves photo but it’s messed up for some reason. Tested on Android and on PC but its the same all time. Here is what I get:

Can Someone Help Me? THANKS IN ADVANCE.

Here is my Code:

using UnityEngine;
using System.Collections;
using System.IO;

public class AndroidCamera : MonoBehaviour
{
	public Vector2 photoSize;
	public int photoFrames;
	public GUIStyle guiStyle;
	
	private WebCamTexture webCamTexture;
	private string camName;

	void Start () 
	{
		WebCamDevice[] devices = WebCamTexture.devices;
		webCamTexture = new WebCamTexture(GetCamera(), (int)photoSize.x, (int)photoSize.y, photoFrames);
		renderer.material.mainTexture = webCamTexture;
		webCamTexture.Play();
		Screen.orientation = ScreenOrientation.Portrait;
	}
	
	void TakePhoto()
	{   
		Texture2D takenPhoto = new Texture2D((int)photoSize.x, (int)photoSize.y, TextureFormat.ARGB32, false);
		
		Color[] texData = webCamTexture.GetPixels();
		
		takenPhoto.SetPixels(texData);
		takenPhoto.Apply();
		
		byte[] photoData = takenPhoto.EncodeToPNG();
		Destroy(takenPhoto);
		
		//if(File.Exists(Application.persistentDataPath + "/AvatarPhoto.png"))
		//{
		//	File.Delete(Application.persistentDataPath + "/AvatarPhoto.png");
		//}
		File.WriteAllBytes(Application.dataPath + "/AvatarPhoto.png", photoData);
	}
	
	void OnGUI()
	{
		if(GUI.Button(new Rect(0, 10, Screen.width, Screen.height / 5), "TAKE PHOTO!", guiStyle))
		{
			TakePhoto();
		}
	}
}

Make sure you put the right image size, try this

Texture2D takenPhoto = new Texture2D((int)renderer.material.mainTexture.width, (int)renderer.material.mainTexture.height);

It worked for me!

I’m positive this is problem with not yielding – it has to do with the computer attempting to perform large operations like WriteAllBytes, on line 39, all in one frame. What I would suggest is that you make TakePhoto an IEnumerator, and try yielding after that line and some of the other large operations in that vicinity. Remember that you can Yield return StartCoroutine(“Somefunction”) to yield until the end of a function, and yield return 0 to skip a frame. But I would just try yield return new WaitForSeconds(1) after a few of the lines, and see if that fixes it – then start removing them. Let us know if that fixes it!

Hello thanks for answer but it doesnt work for me (or i’m doing it wrong).
Here is code the code:

	IEnumerator TakePhoto()
	{   
		webCamTexture.Pause();
		yield return new WaitForSeconds(1.0f);
		
		Texture2D takenPhoto = new Texture2D((int)photoSize.x, (int)photoSize.y, TextureFormat.ARGB32, false);
		yield return new WaitForSeconds(1.0f);
		
		Color[] texData = webCamTexture.GetPixels();
		yield return new WaitForSeconds(1.0f);
		
		takenPhoto.SetPixels(texData);
		yield return new WaitForSeconds(1.0f);
		
		takenPhoto.Apply();
		yield return new WaitForSeconds(1.0f);
		
		byte[] photoData = takenPhoto.EncodeToPNG();
		yield return new WaitForSeconds(1.0f);
		
		Destroy(takenPhoto);
		yield return new WaitForSeconds(2.0f);
		
		File.WriteAllBytes(Application.dataPath + "/AvatarPhoto.png", photoData);
		yield return new WaitForSeconds(2.0f);
		
		Debug.Log("Done");
		yield return 0;
	}

And Here is the Photo:

Thx @ImranZahid!, I had a similar issue, but wasn’t using the correct size, totally missed it here: How to save a snapshot of the WebcamTexture? - Unity Answers , though you both helped me finish this up… @wonker21 I would suggest removing the TextureFormat from your Texture2D declaration, and/or us TextureFormat.RGBA32, but I’m feeling like ImranZahid is right for your case too, where does photoSize come from? Try Grabbing the meshRenderer, material, mainTexture x & y:
MeshRenderer m = gameObject.GetComponent();
m.material.mainTexture.width & .height. GL