2012-03-16 3 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 Forms

답변

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 블록을 사용하여 직접 정리하십시오.