【问题标题】:Image from database to Windows Forms从数据库到 Windows 窗体的图像
【发布时间】:2012-03-16 10:01:39
【问题描述】:
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”。我可能做错了什么,或者我该如何调试和解决这个问题?谢谢!

【问题讨论】:

    标签: .net sql-server winforms


    【解决方案1】:

    使用 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;
    }
    

    【讨论】:

      【解决方案2】:

      不处置您的物品将无济于事。使用try/finally 块或using statement 自行清理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-03
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多