=
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
图片到byte[]再到base64string的转换:
Bitmap bmp = new Bitmap(filepath)
MemoryStream ms = new MemoryStream()
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)//自己更换图片类型
byte[] arr = new byte[ms.Length]
ms.Position = 0
ms.Read(arr, 0, (int)ms.Length)
ms.Close()
string pic = Convert.ToBase64String(arr)
保存pic字符串到数据库即可
数据库读取后转为图片
base64string到byte[]再到图片的转换:
byte[] imageBytes = Convert.FromBase64String(pic)
//读入MemoryStream对象
MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length)
memoryStream.Write(imageBytes, 0, imageBytes.Length)
//转成图片
Image image = Image.FromStream(memoryStream)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)