sql server - Out of Memory in vb.net -
i inserting , retrieving image database. can insert having hard time retrieving file. used varbinary(max) datatype of image.
this code inserting:
dim ms new memorystream picturebox1.image.save(ms, picturebox1.image.rawformat) dim img() byte img = ms.toarray() cmd.commandtext = "insert 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
and how retrieve:
con.open() cmd.commandtext = "select * stud 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 byte() = directcast(dr("pic"), byte()) if not imagedata nothing using ms new memorystream(imagedata, 0, imagedata.length) ms.write(imagedata, 0, imagedata.length) picturebox1.backgroundimage = image.fromstream(ms, true) end using end if end while
my problem is, says out of memory whenever run program. how solve it? size of image retrieving 2mb.
i using memorystream , researched used filestream. believe it's different in ways.
the error due memorystream should open time.
to resolve problem, use following function image byte array
public function bytearraytoimage(bytearrayin byte()) image dim img image = nothing dim ms new memorystream(bytearrayin, 0, bytearrayin.length) ms.write(bytearrayin, 0, bytearrayin.length) img = image.fromstream(ms, true) return img end function
call function fill picturebox1:
picturebox1.image = bytearraytoimage(imagedata)
Comments
Post a Comment