C#-Inserting images into a database

Asked

Viewed 457 times

1

I’m doing a product registry. I can insert the image but when I use a combobox to make all the data appear the image is cut in half.

Here is the code of the insert button:

        byte[] imageBt = null;
        FileStream fstream = new FileStream(this.txtPath.Text, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fstream);
        imageBt = br.ReadBytes((int)fstream.Length);

        string connString = "SERVER=//; Database=//; Uid=//; Pwd=//;";
        string Query = "insert cadastro.tbproduto(nomeProduto,quantProduto,precoProduto,imagemProduto) values ('" + this.txt1.Text + "','" + this.txt2.Text + "','" + this.txt3.Text + "',@IMG);";
        MySqlConnection conDataBase = new MySqlConnection(connString);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;


            conDataBase.Open();
            cmdDataBase.Parameters.Add(new MySqlParameter("@IMG",imageBt));
            myReader = cmdDataBase.ExecuteReader();
            MessageBox.Show("Salvos com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
            while (myReader.Read())
            {

            }  

Here’s the combobox code:

        string connString = "SERVER=//; Database=//; Uid=//; Pwd=//;";
        string Query = "select * from cadastro.tbproduto where nomeProduto = '" +              comboBox1.Text + "';";
        MySqlConnection conDataBase = new MySqlConnection(connString);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;


            conDataBase.Open();

            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {
                string sName = myReader.GetString("nomeProduto");
                string qName = myReader.GetString("quantProduto");
                string pName = myReader.GetString("precoProduto");


                txt1.Text = sName;
                txt2.Text = qName;
                txt3.Text = pName;

                byte[] imgg = (byte[])(myReader["imagemProduto"]);

                    MemoryStream mstream = new MemoryStream(imgg);
                    pictureBox1.Image = System.Drawing.Image.FromStream(mstream);

            }  
  • Have you checked if the image is being saved in half? Take a tuple that has an image, extract base 64 and generate the image from it separately - if it’s half full, it’s saved like this.

No answers

Browser other questions tagged

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