2012-03-16 11 views
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); 
} 

このコードでは、エラーが "Out of memory"と表示されます。私は何が間違っているのでしょうか、それをどのようにデバッグして解決できますか?ありがとう!データベースからWindowsフォームへのイメージ

答えて

2

System.Drawing.Imageクラスを使用してファイルを保存し、この画像を直接画像ボックスに割り当てます。

チェックこの:

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; 
} 
1

オブジェクトを廃棄しないでください。 try/finallyブロックを使用するか、using statementを使用して自分で修復してください。

関連する問題