十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
public Byte[] getphoto(string photopath) //参数图片地址,主要用到的类有FileStream
创新互联建站专注于企业全网整合营销推广、网站重做改版、岳阳县网站定制设计、自适应品牌网站建设、H5场景定制、成都做商城网站、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为岳阳县等各大城市提供网站开发制作服务。
{
string str = photopath;
FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[file.Length];
file.Read(bytBLOBData, 0, bytBLOBData.Length);
file.Close();
return bytBLOBData;
}//这是定义函数..
在access数据库里将字段的类型设置为ole对象
Public img As Byte() '图片处理用的字节数组
img=My.Computer.FileSystem.ReadAllBytes(filePath)'filePath是你图片文件的路径
剩下的就是数据库插入操作了
Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data.mdb")
Dim comm As OleDb.OleDbCommand
comm = New OleDb.OleDbCommand( _
"INSERT INTO Photo(BuFan_F,PhotoNo,Photo) Values('" Me.CobBuFan.Text.Trim "','" Me.txtNo.Text.Trim "',@image)", cn)
'向数据库添加存储了图片数据的二进制数组
comm.Parameters.Add("@image", _
OleDb.OleDbType.Binary, img.Length).Value = img
If cn.State = ConnectionState.Closed Then cn.Open() '打开数据库连接
comm.ExecuteNonQuery() '执行数据库命令
If cn.State = ConnectionState.Open Then cn.Close() '关闭数据库连接
MessageBox.Show("图片成功保存到数据库", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information)
这个做法应该是图方便的加密解密做法。按你的C#代码来改的话是这样的。
'Imports System.IO
Public Function MapPath(ByVal virtualPath As String) As String
' Return System.Web.Hosting.MapPath(virtualPath)
' 猜想是这个 MapPath 函数
' 如果不是那就自己还原原来C#代码里的那个MapPath
End Function
Public Sub GetImage()
Dim s As System.IO.Stream = System.IO.File.Open(MapPath("33.jpg"), System.IO.FileMode.Open)
Dim leng As Integer = 0
If s.Length Int32.MaxValue Then
leng = s.Length
End If
Dim by(leng) As Byte
s.Read(by, 0, leng) ' 把图片读到字节数组中
s.Close()
Dim str As String = Convert.ToBase64String(by) ' 把字节数组转换成字符串
Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(MapPath("11.txt")) ' 存入11.txt文件
sw.Write(str)
sw.Close()
sw.Dispose()
End Sub
' 把字符串还原成图片
Public Sub CreateImg()
Dim sr As New System.IO.StreamReader(MapPath("11.txt"))
Dim s As String = sr.ReadToEnd()
sr.Close()
Dim buf As Byte() = Convert.FromBase64String(s) ' 把字符串读到字节数组中
Dim ms As New System.IO.MemoryStream(buf)
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
img.Save(MapPath("12.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg)
ms.Close()
ms.Dispose()
End Sub