Textures, Base64, Encoding Decoding

Hello everyone, i’m working on a game and i need to code an decode an texture from a sprite.

I’m using System.Convert but it’s not working, here’s the code:

For encode:

string base64Img;

WWW www = new WWW (url);

		base64Img = System.Convert.ToBase64String (www.texture.EncodeToPNG());

For decode:

                byte[] Bytes = System.Convert.FromBase64String (base64Img);
		Texture2D tex = new Texture2D (500, 700);
		tex.LoadImage (Bytes);
		Rect rect = new Rect(0, 0, tex.width, tex.height);
		sprite = Sprite.Create (www.texture, rect, new Vector2() , 100f);
  1. What is it doing instead of working?
  2. How crucial is it to encode it as a string instead of cutting out the middle-man and just assigning www.texture directly to tex? Are you playing with the contents of base64Img to try to glitch it out or something?

I notice your Sprite.Create is partly bypassing the middle-man by referencing www.texture directly instead of tex, so I’m curious.

It would also be worth it to test passing www.texture.EncodeToPNG() directly to tex,LoadImage() to confirm if the string encoding is the problem or something else.

Dosnt show anything.

It’s crucial cos it’s not a standalone game, it’s multiplayer with a server.

I’ve done this before and it’s worked fine. Are you loading a file off the disk that you saved in another application or are you trying to write a file in Unity to disk and then reload it via WWW?

I’m loading of the disk and i want to encode it to load via decode.

Phone1:
-Load from disk
-Encode to string
-Send String to phone 2

phone2:
-decode string
-show image

Why are you bothering with encoding strings? Just send the data directly.

–Eric

My guess is that he’s doing it via RPC which doesn’t accept a byte array but does accept a string.

I have been struggling with this also a pretty long time and I guess you are doing the same mistake

byte[ ] Bytes = System.Convert.FromBase64String(BLACK_MAGIC_STRING);
this function is taking the BASE64 string without any prefix

so THIS IS WRONG
**data:image/png;base64,**iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAgAElEQVR4Xty9B7hlV3Xn+d/73PuqXuWonHNAIiOCyBnTYMA27cCME3gcuu1vPO7xTDc27a+/nmnbM9NjG…

THIS IS CORRECT
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAgAElEQVR4Xty9B7hlV3Xn+d/73PuqXuWonHNAIiOCyBnTYMA27cCME3gcuu1vPO7xTDc27a+/nmnbM9NjG…

Cheers