Read an image in SQL Server for a picturebox in C#

Asked

Viewed 71 times

0

I have Bytearrays of images in a database on Mysql Server and I intend to read them and after that convert Bytearray to an image that can be arranged in a picturebox. I have tried using other methods including a dll, however there were always errors and I thought this way would be simpler.

At the moment the code I have is this:

StringBuilder str5Sql = new StringBuilder();
        str5Sql.Append("SELECT TOP 1000 MOVIMENTO_MATERIAL.IMAGEM FROM MOVIMENTO_MATERIAL INNER JOIN MATERIAL ON  MOVIMENTO_MATERIAL.ID_MATERIAL = MATERIAL.ID WHERE MOVIMENTO_MATERIAL.ID IN (SELECT TOP 1 ID FROM MOVIMENTO_MATERIAL WHERE MOVIMENTO_MATERIAL.ID_MATERIAL = @wantedcell ORDER BY ID DESC )");
        i = 0;
        using (SqlConnection sql5Conn = new SqlConnection(strConexao))
        using (SqlCommand sql5Cmd = new SqlCommand(str5Sql.ToString(), sql5Conn))
        {

            sql5Conn.Open();
            sql5Cmd.Parameters.Add("@wantedcell", SqlDbType.BigInt).Value = this.wantedcell;

            using (SqlDataReader dr5 = sql5Cmd.ExecuteReader())

                while (dr5.Read())
                {

                    byte[] byteimagem = BitConverter.GetBytes(dr5[0]);
                    Image imagem_ultima = Diversos.ByteArrayToImage(byteimagem);
                    pictureBox2.Image = imagem_ultima;



                    //Incrementa uma interação
                    i++;
                }
        }

On the line where I try to store the data in a Bytearray variable I face the following error:

Error   CS1503  Argument 1: cannot convert from 'object' to 'bool'  

Does anyone know any way to let this work properly?

  • 1

    On what line exactly does the error occur? It doesn’t seem to be related to the image.

  • @rubStackOverflow at byte[] byte line = Bitconverter.Getbytes(Dr5[0]);'

  • And why do you change the image of the same component on the screen with each line? What do you expect to accomplish with this?

  • 2

    uses byte[] byteimagem = (byte[])dr5[0]

  • @Rovannlinhalis I just tested this and it worked, thank you very much :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.