Problem with Saving Image in Mysql C#

Asked

Viewed 536 times

3

Hello!

When saving the image, instead of going the file to the database with the right size, goes the following: Aparece isso no banco

Code inside the Visual Studio:

        private void button1_Click_1(object sender, EventArgs e)
    {
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "JPEG Files(*.jpg)|*.jpg";

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            string foto = dialog.FileName.ToString();
            textBox1.Text = foto;
            pictureBox1.ImageLocation = foto;

        }
    }

  private void button3_Click(object sender, EventArgs e)
    {
        byte[] img = new byte[0];

        FileStream Stream = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
        BinaryReader binary = new BinaryReader(Stream);
        img = binary.ReadBytes((int)Stream.Length);


        string comando = "INSERT INTO ibagen(img) VALUES('" + img.ToArray() + "')";
        MySqlCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = comando;


        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Imagem enviada com sucesso!");
        }catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }finally
        {
            con.Close();
        }

    }

I don’t know what else to do, I tried to convert the "image" that went to the database, and that’s it here:

Dá nisso

  • also have an example here: https://answall.com/a/207207207/69359

  • No, nothing to do with the first

  • My mistake is completely different.

  • are right, see if the linked example helps you

  • That’s right, my mistake is exactly this, the image does not go to the database, I don’t know why ;(

1 answer

3


With a button you upload the file to PictureBox, right? Then, at the time of saving, you can take the Image of PictureBox, convert it into byte[] and execute the query passing as parameter.

Example:

Method to convert Image in byte[]:

public static byte[] ConvertImageToByte(System.Drawing.Image image)
{
    if (image == null)
        return null;

    byte[] data;

    using (MemoryStream stream = new MemoryStream())
    {
        Bitmap bmp = new Bitmap(image);
        bmp.Save(stream, ImageFormat.Jpeg);
        data = stream.ToArray();
    }

    return data;
}

Your event would be like this:

private void button3_Click(object sender, EventArgs e)
{
    byte[] bImage = ConvertImageToByte(pictureBox1.Image);

    string sql = "INSERT INTO ibagen (img) VALUES (@img)";

    using (MysqlConnection con = new MysqlConnection("string de conexao"))
    {
        con.Open();
        using (MySqlCommand cmd = new MySqlCommand(sql,con))
        {
            cmd.Parameters.Add("img", OdbcType.Binary); //Aqui trocar pelo type do Mysql
            cmd.Parameters["img"].Value = bImage;
            cmd.ExecuteNonQuery();
        }
    }
}

Remembering that it is just an example. There are no exceptions handling, and the SQL code should not be on the screen.

  • In VALUES (@img), what I do?

  • 1

    Thank you so much! saved me so much

Browser other questions tagged

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