So yesterday i wrote some code to open and save images from unity. I save them as byte in my database. But the problem comes when i try to load them. I’m using Texture2D.LoadImage(byte,bool) for that job but it doesn’t seem to work. I’m getting a red question mark on my image when i try to load something. Every answer will be appreciated.
Here’s the code:
public void LoadFile()
{
IDbCommand comLA = con.CreateCommand();
comLA.CommandText = "SELECT * from pics";
IDataReader reader;
reader = comLA.ExecuteReader();
string bytes = "";
while (reader.Read())
{
bytes = reader.GetString(0);
}
reader.Close();
imageBytes = StringToByteArray(bytes);
ByteArrayToImage();
}
public void ByteArrayToImage()
{
Texture2D imageTex = new Texture2D(1, 1);
imageTex.LoadImage(imageBytes, true);
Sprite sprite = Sprite.Create(imageTex, new Rect(0.0f, 0.0f, imageTex.width, imageTex.height), new Vector2(0.5f, 0.5f), 100.0f);
i.sprite = sprite;
}
public byte[] StringToByteArray(string data)
{
var bytes = System.Text.Encoding.Unicode.GetBytes(data);
return bytes;
}