用的是绑定的数据源 ,想把图片从数据库里面直接读出来,显示到picturebox控件,不知道哪里出的问题显示不出来,也不报错。
private void categoriesBindingSource_CurrentChanged(object sender, EventArgs e)
{
if (_imageBitmap != null)
{
_imageBitmap.Dispose();
}
DataRowView rowView = (DataRowView)categoriesBindingSource.Current;
NorthwindDataSet.CategoriesRow row = (NorthwindDataSet.CategoriesRow)rowView.Row;
byte[] b = (byte[])row.Picture; if (b.Length >0)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
stream.Write(b, 0, b.Length);
pictureBox1.Image = new Bitmap(stream);
stream.Close();
}
}
}
private void categoriesBindingSource_CurrentChanged(object sender, EventArgs e)
{
if (_imageBitmap != null)
{
_imageBitmap.Dispose();
}
DataRowView rowView = (DataRowView)categoriesBindingSource.Current;
NorthwindDataSet.CategoriesRow row = (NorthwindDataSet.CategoriesRow)rowView.Row;
byte[] b = (byte[])row.Picture; if (b.Length >0)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
stream.Write(b, 0, b.Length);
pictureBox1.Image = new Bitmap(stream);
stream.Close();
}
}
}
去掉试试
Image Photo = Image.FromStream(MS, true);
pictureBox1.Image = Photo;
这段代码我注释了,试了的
这个类有个 流成员 Stream _baseStream; // 图片的基地流 // 将数据库来的字节流转换成图片,
// 这里转换成功后直接赋值给 this.Image
Image BytesToImage(byte[] bytes)
{
Image image = null;
_baseStream = new MemoryStream(bytes);
try
{
image = System.Drawing.Image.FromStream(_baseStream);
}
catch (ArgumentException)
{
}
if (null == image)
{
// 注意这里在何种情况下关闭, 销毁 流对象
_baseStream.Dispose();
_baseStream = null;
}
return image;
}