- Home /
How to Convert Image to Texture2d
I'm trying to convert an image to Texture2d. It seems like the following code should work, but the result is a 8x8 Texture2D. How does one do this?
Edit: I should also mention this "Image" is a Vuforia image.
public static Texture2D Image2Texture(Image im)
{
if (im == null)
{
return new Texture2D(4,4);
}
//make a new Texture2D
Texture2D tex = new Texture2D(im.Width, im.Height);
tex.LoadImage(im.Pixels);
return tex;
}
Answer by loihb · Nov 28, 2012 at 05:56 AM
var url1 = new WWW("file://file");
_texture = new Texture2D(256, 256, TextureFormat.DXT5, false);
try {
url1.LoadImageIntoTexture(_texture);
_texture.wrapMode = TextureWrapMode.Clamp;
}catch(err){
Debug.Log(err);
}
This is great, except the image isn't remote, it's an Image object in memory.
I found code here: http://answers.unity3d.com/questions/259781/pixel-byte-array-to-color32-array-combine-zxing-wi.html
that can take bytes[] to convert to Color32[]. But then this code:
tex.SetPixels32(colors);
sets the pixels of the texture to be incorrect.
Your answer
Follow this Question
Related Questions
Shaders: How heavy is tex2D()? 1 Answer
Texture2D Array Appears Grainy 0 Answers
Copying Textures and transparency. 0 Answers
Detect WWW Image bitmap dimensions? 2 Answers