我使用了以下代码。
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);
}
尝试像这样:
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);
}
pictureBox1.Image = Image.FromStream(stream);
- Rahul TripathiTry 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;
}
Image
已被弃用,请使用Varbinary
。 - Baussforeach (DataRow dr1 in dt.Rows)
{
picbyte = (byte[])dr1["AssociateImageData"];
MemoryStream ms1 = new MemoryStream(picbyte);
pictureBox1.Image = Bitmap.FromStream (ms1);
}
这可能是最干净的解决方案,其中内存实际上被正确清理。
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);
}
}
}
}
}