0
private void button2_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=SYS12;Initial Catalog=Teju;Integrated Security=True");
    SqlCommand cmd = new SqlCommand("Select Picture from tblImgData where id= " + listBox1.SelectedValue + " ", con);
    con.Open();
    byte[] byteImg = (byte[])cmd.ExecuteScalar();
    string strfn = Convert.ToString(DateTime.Now.ToFileTime());
    FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
    fs.Write(byteImg, 0, byteImg.Length-1);
    fs.Flush();
    fs.Close();
    pictureBox1.Image = Image.FromFile(strfn);
}

This code showing error as "Out of memory." What might I be doing wrong, or how can I debug and solve this? Thanks!

Rup
  • 33,765
  • 9
  • 83
  • 112
Teju MB
  • 1,333
  • 5
  • 20
  • 37

2 Answers2

2

Use System.Drawing.Image Class to save file then directly assign this image to your picture box.

Check this:

private void button2_Click(object sender, EventArgs e)
{

    SqlConnection con = new SqlConnection("Data Source=SYS12;Initial Catalog=Teju;Integrated Security=True");
    SqlCommand cmd = new SqlCommand("Select Picture from tblImgData where id= " + listBox1.SelectedValue + " ", con);
    con.Open();
    byte[] byteImg = (byte[])cmd.ExecuteScalar();
    string strfn = Convert.ToString(DateTime.Now.ToFileTime());
   Stream stream = new MemoryStream(byteImg);
Image img = System.Drawing.Image.FromStream(stream);

img.Save(strfn , ImageFormat.Jpeg);
    pictureBox1.Image = img;
}
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
1

Not disposing of your objects won't help. Either use the try/finally blocks or a using statement to clean up after yourself.

Bridge
  • 29,818
  • 9
  • 60
  • 82