mysql - Store picture to database; retrieve from db into Picturebox -
hi posted earlier , got still no working solution. have determined last q & there wrong "save db" code "retrieve picture" code. if manually save pic in db stil wont retreive. code patched 3 or 4 examples around net. ideally if had known code , direct me best.
dim filename string = txtname.text + ".jpg" dim filesize uint32 dim imagestream system.io.memorystream imagestream = new system.io.memorystream pbpicture.image.save(imagestream, system.drawing.imaging.imageformat.jpeg) redim rawdata(cint(imagestream.length - 1)) imagestream.position = 0 imagestream.read(rawdata, 0, cint(imagestream.length)) filesize = imagestream.length dim query string = ("insert actors (actor_pic, filename, filesize) values (?file, ?filename, ?filesize)") cmd = new mysqlcommand(query, conn) cmd.parameters.addwithvalue("?filename", filename) cmd.parameters.addwithvalue("?filesize", filesize) cmd.parameters.addwithvalue("?file", rawdata) cmd.executenonquery() messagebox.show("file inserted database successfully!", _ "success!", messageboxbuttons.ok, messageboxicon.asterisk) ![enter image description here][1]
'***retieving picturebox using following code:
private sub getpicture() 'this retrieves pictures mysql db , buffers rawdata memorystream dim filesize uint32 dim rawdata() byte dim conn new mysqlconnection(connstr) conn.open() conn.changedatabase("psdb") dim cmd new mysqlcommand("select actor_pic, filesize, filename actors actor_name = ?autoid", conn) cmd.parameters.addwithvalue("?autoid", actor1box.text) reader = cmd.executereader reader.read() 'data in memory filesize = reader.getuint32(reader.getordinal("filesize")) rawdata = new byte(filesize) {} 'get bytes , filesize reader.getbytes(reader.getordinal("actor_pic"), 0, rawdata, 0, filesize) dim ad new system.io.memorystream(100000) ' dim bm new bitmap ad.write(rawdata, 0, filesize) dim im image = image.fromstream(ad) * "error occurs here" (see below) actor1pic.image = im reader.close() conn.close() conn.dispose() ad.dispose()
well since getting no bashed away @ problem , got work finally. here working code.
save mysql out of picturebox (pbpicture)
dim filename string = txtname.text + ".jpg" dim filesize uint32 conn.close() dim mstream new system.io.memorystream() pbpicture.image.save(mstream, system.drawing.imaging.imageformat.jpeg) dim arrimage() byte = mstream.getbuffer() filesize = mstream.length dim sqlcmd new mysqlcommand dim sql string mstream.close() sql = "insert [your table] (picture, filename, filesize) values(@file, @filename, @filesize)" try conn.open() sqlcmd .commandtext = sql .connection = conn .parameters.addwithvalue("@filename", filename) .parameters.addwithvalue("@filesize", filesize) .parameters.addwithvalue("@file", arrimage) .executenonquery() end catch ex exception msgbox(ex.message) conn.close() end try load mysql db picturebox
dim adapter new mysqldataadapter adapter.selectcommand = cmd data = new datatable adapter = new mysqldataadapter("select picture [yourtable]", conn) note!! can put 1 picture in picturebox query can return 1 record you
commandbuild = new mysqlcommandbuilder(adapter) adapter.fill(data) dim lb() byte = data.rows(0).item("picture") dim lstr new system.io.memorystream(lb) pbpicture.image = image.fromstream(lstr) pbpicture.sizemode = pictureboxsizemode.stretchimage lstr.close()
Comments
Post a Comment