跪求解c#读取图片问题

来源:百度知道 编辑:UC知道 时间:2024/07/02 04:01:33
byte[] imagebytes = null;
string str = "server=.;database=student;uid=sa;pwd=";
string sql = "select top 1 * from student";
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(1);
}
dr.Close();
cmd.Clone();
con.Close();
MemoryStream ms = new MemoryStream(imagebytes,true);
pictureBox1.Image = Image .FromStream (ms);
为什么老是报参数无效的错误呢?我的数据库表结构为Id,studentimage(image)的,都是书上的代码啊,怎么我这里就运行不了呢?

代码是没有问题的,估计是其它出了问题,哪个地方报参数错误,你把错误贴出来看看!

cmd.Clone();
晕了

将二进制数组为转换照片

public Bitmap ByteArrayToPhoto(Binary touXiang)
{
byte[] bufferPhoto = touXiang.ToArray();
Bitmap image = new Bitmap(new MemoryStream(bufferPhoto));
return image;
}

cmd.Clone();
高,读取每一个读取过程就要执行图片显示一次

我都只把图片存放在文件夹中,数据库里面存地址。你这个书上的方法,不太实用。而且,很多书上的东西都是没经过测试的。

你数据库里的字段studentimage(image)设置为string类型更容易实现些,把图片放在项目的一个img文件夹里,数据库里存放的是一个地址~/img/1001.jpg
string filepath = FileUploadproduct.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
string savePath = Request.PhysicalApplicationPath + "img\\";
this.FileUploadproduct.SaveAs(savePath + filename);
this.Imgnews_img.ImageUrl = "~/img/" + f