- Home /
Resize Texture2D comes out grey
I'm trying to resize a downloaded image but all resized images come out solid grey. Can someone please explain why?
public void AddPhotosToList(WWW www)
{
Texture2D uploadedPhoto = www.texture;
uploadedPhoto.Resize(256,256);
uploadedPhoto.Apply();
memberPhotos.Add(uploadedPhoto);
SavePhotosToCache(uploadedPhoto);
}
Also, I'd rather crop the texture within the boundary of 256,256. But I cant find any functions in the documentation. If anyone has any insights on that as well.
Answer by Eric5h5 · Nov 14, 2012 at 04:27 PM
As the docs say about Resize: "After resizing, texture pixels will be undefined." It just resizes the texture, it does not scale the contents. You can use this instead.
It does work in the webplayer. Or at least the version I posted did; looks like someone added to it using things that don't work in the webplayer subset. I've restored it so it works in the webplayer again, and while I was at it I changed the Color stuff to Color32 so it's faster. (This is for the JS version; there's also a C# version which someone else did, so that would have to be updated as well if you want it to work in the webplayer.)
Thanks a lot Eric! I have been trying to solve this issue for 2 months now! Will those two functions work on other Platforms ($$anonymous$$AC for example) or do I have to make platform-specific code regions to handle different platform-cases?
Answer by Fattie · Mar 01, 2016 at 01:45 AM
Regarding Eric5h5's famous and awesome TextureScale, here's some example code that shows a use of it, and indeed how to crop.
For google: in Unity3D take a photo from your iPhone or Android webcam device camera, take a photo on to a WebCamTexture and then scale and crop the image and save to disk.
It's all a piece of cake with TextureScale !!
public WebCamTexture wct;
public void UserClickedTakePhoto()
{
// take the photo. scale down to 256
// and crop to a central-square
// !!NOTE!! for simplicity this demo code
// assumes the tetxture is wider than high!
//consider... yield return new WaitForEndOfFrame();
Debug.Log("is " +wct.width +" h" +wct.height );
int oldW = wct.width; // assume it's wider than high
int oldH = wct.height;
Texture2D photo = new Texture2D(oldW, oldH,
TextureFormat.ARGB32, false);
photo.SetPixels( 0,0,oldW,oldH, wct.GetPixels() );
photo.Apply();
Debug.Log("took photo !");
Debug.Log("is " +photo.width +" h" +photo.height );
int newH = 256;
int newW = Mathf.FloorToInt(
((float)newH/(float)oldH) * oldW );
TextureScale.Bilinear(photo, newW,newH);
Debug.Log("scaled !");
Debug.Log("is " +photo.width +" h" +photo.height );
// finally crop to central square 256.256
int startAcross = (newW - 256)/2;
Debug.Log("starting across at: " +startAcross);
Color[] pix = photo.GetPixels(startAcross,0, 256,256);
photo = new Texture2D(256,256, TextureFormat.ARGB32, false);
photo.SetPixels(pix);
photo.Apply();
Debug.Log("'SetPixels' cropped !");
Debug.Log("is " +photo.width +" h" +photo.height );
demoImage.texture = photo;
// consider
// System.IO.File.WriteAllBytes(
// Application.persistentDataPath+"p.png",
// photo.EncodeToPNG());
// or
// byte[] bytes = photo.EncodeToPNG();
// File.WriteAllBytes(
// Application.persistentDataPath+"p.png", bytes);
}
Year in and year out, TextureScale
is still the best and only way to scale your PNG and Texture2D in Unity3D! Hope it saves someone some typing.
I can't find the TextureScale component. And unity wiki link of the original response is dead. Any idea on how to resolve this now?
Maybe this can help: https://gist.github.com/gszauer/7799899
Your answer
Follow this Question
Related Questions
Scaling / Resizing an Image (Texture2D) 6 Answers
Resizing GUI textures 0 Answers
How to shorten/extend an object? 2 Answers
Resize object in runtime 1 Answer
Texture2D scale - iTween 1 Answer