Crop Texture2D - Get/Set Pixels

Hello,

I’m working on a Crop system for my Texture2Ds, and I can’t seem to get it to work, not sure what I’m doing wrong here:

//original image (before crop)
var oImg : Texture2D = fromQuad.material.mainTexture;
var pixels : Color[] = oImg.GetPixels(0, 0, oImg.width, oImg.height, 0); 


 //CROP IMAGE TO FIT SQUARE RATIO (cropSize)
var cropSize = 500;
var img : Texture2D = new Texture2D(cropSize, cropSize, TextureFormat.RGB24, false);
    
img.SetPixels(0, 0, cropSize, cropSize, pixels, 0);
img.Apply();
    
toQuad.material.mainTexture = img;

Any ideas? Thanks

Simple way of image Cropping in unity 5.5

public Texture2D mTexture;
void Start () {
	Color[] c = mTexture.GetPixels (0, 0, 200, 200);
	Texture2D m2Texture = new Texture2D (200, 200);

	m2Texture.SetPixels (c);
	m2Texture.Apply ();

	gameObject.GetComponent<MeshRenderer> ().material.mainTexture = m2Texture;

}

SetPixels() requires that the array is the correct size for the image (also keeping mip maps in mind). You are first getting the array of the original image in its original size, then you declare a new texture with a different resolution and you try to cram the same pixels in the new space. Instead, use oImg.GetPixels(0,0,cropSize, cropSize,0).