- Home /
how to set Image that converted from Base64 string to Sprite
I get a string from server that save an image in Base64 string and i convert to Image like this:
byte[] imageBytes = Convert.FromBase64String(str);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image img= System.Drawing.Image.FromStream(ms, true);
// img does not contain a definition for 'texture'
then i want to set this 'img' to a Game Object Image runtime and for this a need to get sprite of this img or create one sprite from img texture but the problem is this ims is from using System.Drawing and there is not texture or sprite and it should convert to type of UnityEngine.UI
Comment
Best Answer
Answer by Hellium · Apr 08, 2019 at 01:30 PM
Try:
byte[] imageBytes = Convert.FromBase64String(str);
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage( imageBytes );
Sprite sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);