3

I have an image in a sql table that is converted to byte array.How can i display it in a picture box when i click on the record from a dataGridView that contains the table?I need the actual code.Thanks.This is the code i have for the conversion:

private void button2_Click(object sender, EventArgs e)
{
    SqlConnection cn = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True");
    MemoryStream ms = new MemoryStream();
    pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

    byte[] pic_arr = new byte[ms.Length];
    ms.Position = 0;
    ms.Read(pic_arr, 0, pic_arr.Length);

    SqlCommand cmd = new SqlCommand("insert into motociclete(firma,model,poza,pret,anf,greutate,caprez,putere,garantie,stoc) values (@firma,@model,@poza,@pret,@anf,@greutate,@caprez,@putere,@garantie,@stoc)",cn);
    cmd.Parameters.AddWithValue("@firma", textBox3.Text);
    cmd.Parameters.AddWithValue("@model", textBox10.Text);
    cmd.Parameters.AddWithValue("@poza", pic_arr);
    cmd.Parameters.AddWithValue("@pret", textBox7.Text);
    cmd.Parameters.AddWithValue("@anf", textBox4.Text);
    cmd.Parameters.AddWithValue("@greutate", textBox9.Text);
    cmd.Parameters.AddWithValue("@caprez", textBox5.Text);
    cmd.Parameters.AddWithValue("@putere", textBox8.Text);
    cmd.Parameters.AddWithValue("@garantie", textBox6.Text);
    cmd.Parameters.AddWithValue("@stoc", textBox2.Text);

    cn.Open();

    try
    {
        int rez = cmd.ExecuteNonQuery();
        if (rez > 0)
        {
            MessageBox.Show("Adaugare reusita ");
        }
    }
    catch (Exception)
    {
        MessageBox.Show("Inregistrarea exista deja ");
    }
    finally
    {
        cn.Close();
        obj.loaddata();
        this.Close();
    }
}
2
  • 1
    Can you show the code you have now? It's a lot easier to help you if we see how you're trying to solve this. Commented May 18, 2013 at 23:28
  • i only have the code that converts the image into byte.I'll post that. Commented May 18, 2013 at 23:29

1 Answer 1

5

Assuming that picture is a byte[] that is fetched from database:

byte[] picture = // read from db
PicBoxImage.Image = Image.FromStream(new MemoryStream(picture));                                
PicBoxImage.Refresh();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.