How to create a sprite from an image retrieved from mySQL database?
Hello everyone,
I want to create a sprite using an image that I retrieved from a database. The code to get the image is:
byte[] GetImage(string Nome)
{
string ConnectionString = "datasource=********;port=*****;username=****;password=*****";
using (var conn = new MySqlConnection(ConnectionString))
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT foto FROM catraport.utilizador WHERE nome = ?";
cmd.Parameters.AddWithValue("?", Nome);
conn.Open();
using (var reader = cmd.ExecuteReader())
{
if (!reader.Read())
{
return null;
}
const int CHUNK_SIZE =2*1024;
byte[] buffer = new byte[CHUNK_SIZE];
long bytesRead;
long fieldOffset = 0;
using (var stream = new MemoryStream())
{
while ((bytesRead = reader.GetBytes(reader.GetOrdinal("foto"), fieldOffset, buffer, 0, buffer.Length)) == buffer.Length)
{
stream.Write(buffer, 0, (int)bytesRead);
fieldOffset += bytesRead;
}
return stream.ToArray();
}
}
}
}
Then, I create a texture with the bytes that I got from the function above like:
private Button button;
byte[] image = GetImage("Mário Fernandes");
Texture2D texture = new Texture2D(8,8);
texture.LoadRawTextureData(image);
texture.Apply();
Sprite foto = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width / 2, texture.height / 2));
button.GetComponent<Image>().sprite= foto;
When i put the program to run It does not put the image on the button's sprite, instead the following image appears:
The image that I want is in png format.
Does anyone knows why this is happening?
Thanks.
sem-titulo.png
(98.6 kB)
Comment