byte转换成image

来源:百度知道 编辑:UC知道 时间:2024/06/28 07:24:59
我要将图片存入数据库,部分代码如下
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto = UpPhoto.PostedFile;
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
System.IO.Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);
下面这句是用来传参数的
images.Picture = PhotoArray;
Picture是image类型的
执行时报错:无法将类型 System.Drawing.Image隐式转换为ystem.Web.UI.WebControls.image

我之前也上网搜过类似的问题,但是没有解答出来。
请问怎么解决这个问题啊?谢谢了!

不知道你这句话是真么意思,要存入数据库,现在拿MSSQL 举例:
得到 PhotoArray后,用存储过程就行了,用动态sql我试了几次发现不行,具体代码如下(测试通过):
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=xnshare");
SqlCommand comm = new SqlCommand("insert into img_table(img) values (@image)", conn);

comm.Parameters.Add("@image", SqlDbType.Image, bytes.Length).Value = PhotoArray;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();