public Byte[] getphoto(string photopath) //參數(shù)圖片地址,主要用到的類有FileStream
創(chuàng)新互聯(lián)建站專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、岳陽縣網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場景定制、成都做商城網(wǎng)站、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為岳陽縣等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
{
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;
}//這是定義函數(shù)..
在access數(shù)據(jù)庫里將字段的類型設(shè)置為ole對象
Public img As Byte() '圖片處理用的字節(jié)數(shù)組
img=My.Computer.FileSystem.ReadAllBytes(filePath)'filePath是你圖片文件的路徑
剩下的就是數(shù)據(jù)庫插入操作了
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)
'向數(shù)據(jù)庫添加存儲了圖片數(shù)據(jù)的二進制數(shù)組
comm.Parameters.Add("@image",?_
OleDb.OleDbType.Binary,?img.Length).Value?=?img
If?cn.State?=?ConnectionState.Closed?Then?cn.Open()?'打開數(shù)據(jù)庫連接
comm.ExecuteNonQuery()?'執(zhí)行數(shù)據(jù)庫命令
If?cn.State?=?ConnectionState.Open?Then?cn.Close()?'關(guān)閉數(shù)據(jù)庫連接
MessageBox.Show("圖片成功保存到數(shù)據(jù)庫",?"完成",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
這個做法應(yīng)該是圖方便的加密解密做法。按你的C#代碼來改的話是這樣的。
'Imports System.IO
Public Function MapPath(ByVal virtualPath As String) As String
' Return System.Web.Hosting.MapPath(virtualPath)
' 猜想是這個 MapPath 函數(shù)
' 如果不是那就自己還原原來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) ' 把圖片讀到字節(jié)數(shù)組中
s.Close()
Dim str As String = Convert.ToBase64String(by) ' 把字節(jié)數(shù)組轉(zhuǎn)換成字符串
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) ' 把字符串讀到字節(jié)數(shù)組中
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