byte[] picData = null
while (sdr.Read())
{
picData = (byte[])sdr["PictureData"]
}
sdr.Close()
System.IO.MemoryStream ms = new System.IO.MemoryStream(picData)
System.Drawing.Image img = System.Drawing.Image.FromStream(ms)
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)上面用到的保存和读取的函数(使用存储过程,把存储过程名车鞥换成SQL语句一样的效果) public int AddPicture(string picDesn, byte[] picData)
{
DB db = new DB()//建立数据库连接
SqlParameter[] parms = { new SqlParameter("@PictureData", SqlDbType.VarBinary),
new SqlParameter("@PictureID", SqlDbType.Int,4)}
parms[0].Value = picData
parms[1].Direction = ParameterDirection.Output
if (db.ExecuteNonQuery(CommandType.StoredProcedure, "Picture_ADD", parms) >0)//添加 *** 作
{
return (int)parms[1].Value
}
return 0
}
public SqlDataReader GetPicture(int picID)
{
DB db = new DB()//建立数据库连接SqlParameter[] parms = { new SqlParameter("@PictureID", SqlDbType.Int,4)}
parms[0].Value = picID SqlDataReader sdr = db.ExecuteReader(CommandType.StoredProcedure, "Picture_GetModel", parms)//读取图像二进制数据
return sdr
} 补充:获取图像数据最好单独写在一个页面里,通过传递参数的方式获取图像并显示在Html控件中。
this.pictureBox1.Image=
Image.FromStream(this.openFileDialog1.OpenFile())
//获取当前图片的路径
string
path
=
openFileDialog1.FileName.ToString()
//将制定路径的图片添加到FileStream类中
FileStream
fs
=
new
FileStream(path,
FileMode.Open,
FileAccess.Read)
//通过FileStream对象实例化BinaryReader对象
BinaryReader
br
=
new
BinaryReader(fs)
//通过BinaryReader类对象的ReadBytes()方法将FileStream类对象转化为二进制数组
byte[]
imgBytesIn
=
br.ReadBytes(Convert.ToInt32(fs.Length))第二步:
//将图片添加到数据库中
string
sql="insert
into
pic
values(@pic)"
SqlParameter[]
param
=
new
SqlParameter[]
{
new
SqlParameter("@pic",
imgBytesIn)
}
DBHelper.GetExecuteQuery(sql,
param)第三步:
//将图片从数据库中取出
string
sql="select
*
from
pic
where
id=0"
SqlDataReader
reader
=
DBHelper.GetExecuteReader(sql,
null)
MemoryStream
mss
=
null
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)