Image.FromStream参数无效
SqlConnectioncon=newSqlConnection("server=.;database=xiaoluo1;integratedsecurity=true...
SqlConnection con = new SqlConnection("server=.;database=xiaoluo1;integrated security=true");
SqlCommand cmd = new SqlCommand("select * from photo", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
MemoryStream ms = new MemoryStream((byte[])reader["照片"]);
Image image = Image.FromStream(ms);
reader.Close();
con.Close();
pictureBox1.Image = image; 展开
SqlCommand cmd = new SqlCommand("select * from photo", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
MemoryStream ms = new MemoryStream((byte[])reader["照片"]);
Image image = Image.FromStream(ms);
reader.Close();
con.Close();
pictureBox1.Image = image; 展开
3个回答
展开全部
这个问题前面的回答是对路的,问题确实出在入库而不在读取上,入库时候流没处理好,我曾经遇到的错误是这样的:
MemoryStream stream = new MemoryStream();
PictureBox1.Image.Save(stream, ImageFormat.Bmp ); //这里出错,原图片格式是JPEG
byte [] bytestream = new byte [ stream.Length ];
stream .Read (bytestream ,0,bytestream .Length );
stream.Flush();
//插入数据库新记录
DataAdapter.InsertQuery(bytestream);
这样,读数据显示的时候就出现了Image.FromStream参数无效的错误。
多次测试后发现是自作聪明定义了流转换的格式,改成下面这样后,读数据库图片时候参数错误的问题就消失了:
MemoryStream stream = new MemoryStream();
PictureBox1.Image.Save(stream,PictureBox1 .Image .RawFormat );
byte[] bytestream = stream.ToArray();
stream.Flush();
//插入数据库新记录
DataAdapter.InsertQuery(bytestream);
MemoryStream stream = new MemoryStream();
PictureBox1.Image.Save(stream, ImageFormat.Bmp ); //这里出错,原图片格式是JPEG
byte [] bytestream = new byte [ stream.Length ];
stream .Read (bytestream ,0,bytestream .Length );
stream.Flush();
//插入数据库新记录
DataAdapter.InsertQuery(bytestream);
这样,读数据显示的时候就出现了Image.FromStream参数无效的错误。
多次测试后发现是自作聪明定义了流转换的格式,改成下面这样后,读数据库图片时候参数错误的问题就消失了:
MemoryStream stream = new MemoryStream();
PictureBox1.Image.Save(stream,PictureBox1 .Image .RawFormat );
byte[] bytestream = stream.ToArray();
stream.Flush();
//插入数据库新记录
DataAdapter.InsertQuery(bytestream);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询