找到你要的答案

Q:How to show image into picture box from database?

Q:如何从数据库中显示图片到图片框?

I have use following code.

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

我使用以下代码。

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

Try like this:

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

这样尝试:

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);
} 
answer2: 回答2:

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

试试这个,它适合我:

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;
}
answer3: 回答3:

This is probably the cleanest solution, where the memory is actually cleaned properly.

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

Could also be done like this if you want to specify the column or use other columns within the row.

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

这可能是最干净的解决方案,在记忆实际上是适当的清洁。

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);
                }
            }
        }
    }
}
answer4: 回答4:
foreach (DataRow dr1 in dt.Rows)
{
   picbyte = (byte[])dr1["AssociateImageData"];
   MemoryStream ms1 = new MemoryStream(picbyte);
   pictureBox1.Image = Bitmap.FromStream (ms1);
}
foreach (DataRow dr1 in dt.Rows)
{
   picbyte = (byte[])dr1["AssociateImageData"];
   MemoryStream ms1 = new MemoryStream(picbyte);
   pictureBox1.Image = Bitmap.FromStream (ms1);
}
c#