Load BLOB to Image C# Mysql

Asked

Viewed 725 times

2

I am trying to load an image saved in Mysql, however still unsuccessful. With the code below I can display all other fields. Every time I try to add some method to display the image, some error occurs that does not display any search information.

MySqlConnection conn = new MySqlConnection("Server=localhost;Database=portaria;Uid=root;Pwd=''");
                conn.Open();

                //Inserindo um comando para acessar o funcionário cadastrado
                MySqlCommand consultar = new MySqlCommand(
                    "SELECT funcID, funcRG, funcNome, funcEmpresa, funcSetor, funcRamal, funcFoto, funcStatus " +
                    "FROM funcionario WHERE funcRG = ?", conn);

                consultar.Parameters.Clear();
                consultar.Parameters.Add("@funcRG",MySqlDbType.VarChar, 12).Value = txtRG.Text;
                consultar.CommandType = CommandType.Text;

                //Recebendo o conteúdo da pesquisa
                MySqlDataReader info;
                info = consultar.ExecuteReader();
                info.Read();

                //Passando as informações para os campos
                lblNumID.Text = info.GetString(0);
                txtNome.Text = info.GetString(2);
                txtEmpresa.Text = info.GetString(3);
                txtSetor.Text = info.GetString(4);
                txtFone.Text = info.GetString(5);
                //pictureCadastro.Text = info.GetString(6); ------>>>>> Falta carregar a imagem
                cBoxStatus.Text = info.GetString(7);

                conn.Close();

Can someone help me?

  • 1

    Pera, you want to play the image on a text field?

  • First, you are using the method GetString() if the column stores a blob, it should be the GetBytes()... after, you are trying to play this content in a TextBox? An error message is displayed?

  • I want to play the image in a pictureBox, I use the same element to save the image.

2 answers

0

string base64string = info.GetString(6);
byte[] blobImage = Convert.FromBase64String(base64string);

using (MemoryStream ms = new MemoryStream(blobImage))
{
 pictureCadastro.Image = Image.FromStream(ms);
}

  • As I’m still learning to develop in the language, I’m still having a little trouble and I still can’t figure out how to use the code above. Can I include in the code I pasted in the post so that it runs right on the button I created? "Blob_string" is the variable I converted from image to BLOB?

  • I changed the answer to work according to your code. Add and test.

  • Explaining quickly, I recover the value and store in the variable base64string and then transform it into an array of bytes, then transform the array of bytes into an image using Stream.

  • I ran the test, but unfortunately the image doesn’t show up. Before placing the code the information of this field (cBoxStatus.Text = info.Getstring(7);) appears, but after entering the code, it does not appear and I also have an error message (I am using the question code in a Try/catch). I wonder what I’m doing wrong?

0


I was able to solve my problem using the link code of the site Code Project.

Browser other questions tagged

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