Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a byte[] out of the database and convert it to an image?

I have a stored procedure that return a varbinary(max) type data. I want to convert that data into an Image.

But I have problems with this line:

public Image CargarAvatar(string login)
        {
            System.Object[] Args = new System.Object[1];
            Args[0] = login;

            DataTable X = new DataTable();
            X = TraerDataTable("sp_CargarAvatarUsuario", Args);

            byte[] ImagemByte = Convert.to (X.Rows[0][0].ToString());


            MemoryStream ms = new MemoryStream();
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

Please help! :D

like image 292
Sergio Tapia Avatar asked Dec 18 '22 06:12

Sergio Tapia


1 Answers

A varbinary field is returned as a byte array, so you only need to cast it:

byte[] ImagemByte = (byte[])X.Rows[0][0];

Then you use the array to create the memory stream:

MemoryStream ms = new MemoryStream(ImagemByte);
like image 155
Guffa Avatar answered Jan 13 '23 12:01

Guffa