如何从数据库中将图像显示在图片框中?

4

我使用了以下代码。

foreach (DataRow dr1 in dt.Rows)
{
   picbyte = (byte[])dr1["AssociateImageData"];
   Stream stream = new MemoryStream(picbyte);
   pictureBox1.Image =Image.FromStream(stream,true,true);
}

我遇到了一个错误:“参数无效”。 - pramod narke
你在哪里遇到了这个错误?你尝试过调试吗? - Shaminder Singh
2
为什么你要传递“true,true”到这个方法中,而不只是byte[]?我认为这个重载不存在。 - CRice
但是当我把它移除后,它就无法工作。 - pramod narke
4个回答

1
尝试像这样:

尝试像这样:

var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "AssociateImageData");
int count = ds.Tables["AssociateImageData"].Rows.Count;

if (count > 0)
{ 
    var picbyte = (Byte[])(ds.Tables["AssociateImageData"].Rows[count - 1]["AssociateImageData"]);
    var stream = new MemoryStream(picbyte);
    pictureBox1.Image= Image.FromStream(stream);
} 

@pramodnarke:- 你试过改变这个吗:pictureBox1.Image = Image.FromStream(stream); - Rahul Tripathi
先生,我需要将图像加载到PictureBox中。在调试时,pictureBox1.Image = Image.FromStream(stream); 中的Image属性为空。 - pramod narke
@pramodnarke:请查看此链接:http://www.aspsnippets.com/Articles/Display-Images-from-SQL-Server-Database-using-ASP.Net.aspx - Rahul Tripathi
@pramodnarke:- 还可以参考这个链接:http://stackoverflow.com/questions/22555127/image-file-from-sql-data-table-to-aspimage-control - Rahul Tripathi

1

Try this, it works for me:

private void viewSnapShotButton_Click(object sender, EventArgs e)
{
    this.Cursor = Cursors.WaitCursor;
    string connectionString = ConfigurationManager.AppSettings["myCconnectionSstring"];

    string queryString = ConfigurationManager.AppSettings["MyQueryString"];
    MemoryStream stream = new MemoryStream();
    SqlConnection connection = new SqlConnection(connectionString);
    try
    {
        connection.Open();
        SqlCommand command = new SqlCommand(queryString, connection);
        byte[] image = (byte[])command.ExecuteScalar();
        MemoryStream ms1 = new MemoryStream(image);
        exceptionPictureBox.Image = Bitmap.FromStream(ms1);  //this is how it should be.  I was using Image.FromStream and was getting error.
    }
    finally
    {
        connection.Close();
        stream.Close();
    }
    this.Cursor = Cursors.Default;
}

在 SQL Server 中存储图像应使用哪种数据类型?我可以同时使用 image 或 varbinary(max) 吗? - pramod narke
Image已被弃用,请使用Varbinary - Bauss

0
foreach (DataRow dr1 in dt.Rows)
{
   picbyte = (byte[])dr1["AssociateImageData"];
   MemoryStream ms1 = new MemoryStream(picbyte);
   pictureBox1.Image = Bitmap.FromStream (ms1);
}

0

这可能是最干净的解决方案,其中内存实际上被正确清理。

using (var conn = new SqlConnection(connectionString))
{
    conn.Open();
    using (var comm = new SqlCommand(queryString, conn)
    {
        using (var ms = new MemoryStream((byte[])comm.ExecuteScalar()))
        {
            pictureBox1.Image = Image.FromStream(ms);
        }
    }
}

如果您想指定列或在行内使用其他列,也可以这样做。

using (var conn = new SqlConnection(connectionString))
{
    conn.Open();
    using (var comm = new SqlCommand(queryString, conn)
    {
        using (var reader = new comm.ExecuteReader())
        {
            if (reader.Read())
            {
                using (var ms = new MemoryStream((byte[])reader["Column"]))
                {
                    pictureBox1.Image = Image.FromStream(ms);
                }
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接