image 问题
将image 存入数据库及显示遇到了一些问题,我现在将他简化为显示image,将image 读入byte[],(其他的就是读写数据库),然后将byte[]转化为stream,显示在另外一个pictureBox,(image读写,网上的例子都是这么说的)但现在出错。
if (openFileDialog1.ShowDialog()==DialogResult.OK ){Stream ms=openFileDialog1.OpenFile ();pictureBox1.Image =Image.FromStream(ms) ; //正常byte[] imgbytes=new byte[ms.Length ] ;ms.Read(imgbytes,0,(Int32)ms.Length); Stream msview = new MemoryStream(imgbytes); pictureBox2.Image =Image.FromStream(msview) ; //出错:“其他信息: 使用了无效参数。”
if(openFileDialog1.ShowDialog()==DialogResult.OK) { System.IO.FileStream Fs =new System.IO.FileStream(openFileDialog1.FileName,System.IO.FileMode.Open); byte[] imgbytes =new byte[Fs.Length]; Fs.Read(imgbytes,0,imgbytes.Length); Fs.Close(); System.IO.Stream msview = new System.IO.MemoryStream(imgbytes); pictureBox1.Image =Image.FromStream(msview); }
[解决办法]
数据库读取:
foreach (DataRow ddr in ds.Tables[0].Rows)
{
if (ddr["图片"] != DBNull.Value)
{
byte[] bytes = (byte[])ddr["图片"];
MemoryStream buf = new MemoryStream(bytes);
Image image = Image.FromStream(buf, true);
this.pictureBox1.Image = image;
}
else
this.pictureBox1.Image = null;
}