我正在從我的數據庫插入和檢索圖像。我現在可以插入,但我很難檢索文件。我使用varbinary(max)作爲圖像的數據類型。vb.net中的內存不足
這是我插入代碼:
Dim ms As New MemoryStream
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim img() As Byte
img = ms.ToArray()
cmd.CommandText = "insert into stud values ('" & studno.Text & "', '" & password.Text & "', '" & fname.Text & "', '" & mname.Text & "', '" & lname.Text & "', @img, '" & gender.Text & "', '" & mm.Text & "/" & dd.Text & "/" & yyyy.Text & "', '" & phone.Text & "', '" & address.Text & "', 'Student', '" & secquest.Text & "', '" & answersq.Text & "', '" & TextBox1.Text & "', '" & ComboBox1.Text & "')"
cmd.Parameters.Add("@img", SqlDbType.VarBinary).Value = img
,這是我如何檢索:
con.Open()
cmd.CommandText = "select * from stud where studentno = 'mnb'"
cmd.Connection = con
dr = cmd.ExecuteReader()
While dr.Read()
studnum.Text = dr.Item("studentno")
fname.Text = dr.Item("fname")
mname.Text = dr.Item("mname")
lname.Text = dr.Item("lname")
gender.Text = dr.Item("gender")
section.Text = dr.Item("seccode")
bday.Text = dr.Item("bday")
phone.Text = dr.Item("phoneno")
address.Text = dr.Item("maddress")
Dim imageData As Byte() = DirectCast(dr("pic"), Byte())
If Not imageData Is Nothing Then
Using ms As New MemoryStream(imageData, 0, imageData.Length)
ms.Write(imageData, 0, imageData.Length)
PictureBox1.BackgroundImage = Image.FromStream(ms, True)
End Using
End If
End While
我的問題是,它說內存不足每當我運行我的程序。如何解決它?我正在檢索的圖像的大小是2MB。
我正在使用memorystream和我研究了最常用的文件流。我相信它在某些方面有所不同。
[內存不足Image.FromFile](http://stackoverflow.com/questions/3848132/out-of-memory -image-fromfile) – GSerg
對於一個很大的圖像,考慮將圖像歸檔到某個地方,只保存文件名。您的讀者設置爲在循環中讀取,這意味着您可以創建多個圖像 - 以前沒有配置過 – Plutonix
只需將少量RAM棒添加到您的盒子 –