保存图片的问题,谢指点
Dim ms As Stream
Dim picbyte() As Byte
openFileDialog1.ShowDialog()
If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
ms = openFileDialog1.OpenFile()
ReDim picbyte(ms.Length)
ms.Position = 0
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length))
Dim conn As New SqlConnection
conn.ConnectionString = "server=(local);database=Haha;integrated security=sspi; "
Dim cmd As New SqlCommand
cmd = conn.CreateCommand
conn.Open()
cmd.Parameters.Add( "@image ", SqlDbType.Image)
cmd.Parameters( "@image ").Value = picbyte
cmd.CommandText = "insert aa values(3,2,@image) "
'cmd.CommandText = "insert aa values(3,2, ' " & picbyte(ms.Length) & " ') "
' cmd.CommandText = "select * from aa "
cmd.ExecuteScalar() '这里提示有错? aa表有三个字段,第三个为image型
MsgBox( "插入成功 ")
conn.Close()
ms.Close()
------解决方案--------------------哥们整理了一个在命令控制台下图片和二进制码相互转换的代码,是C#语言的,VB我实在头疼,你自己看着改吧,存到数据库里要用Binary格式,别滴是不可以滴
需要引入io命名空间哦。呵呵
在当前目录要存放一个名为“kevin.gif”的图片
//定义流
FileStream fs = new FileStream(@ "../../kevin.gif ",FileMode.Open);
byte[] by = new byte[fs.Length];
//转换成二进制码,并存储到数组中
fs.Read(by,0,(int)fs.Length);
//将图片从二进制码重新转换回图片格式
fs = new FileStream(@ "../../waston.bmp ",FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
fs.Write(by,0,by.Length);
fs.Close();