vb.net mysql存储图片,VB.net-从mysql数据库直接向/从Picturebox插入/检索图片

I'm am having a heck of a time finding a code snippet that works for this. I have got to the point where it appears the picture is stored as a blob (perhaps incorrectly) by using this code.

Dim filename As String = txtName.Text + ".jpg"

Dim FileSize As UInt32

Dim ImageStream As 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 As String = ("insert into 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 into database successfully!", _

"Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

but on retrieving to the picturebox using the following code:

Private Sub GetPicture()

'This retrieves the pictures from a mysql DB and buffers the rawdata into a memorystream

Dim FileSize As UInt32

Dim rawData() As Byte

Dim conn As New MySqlConnection(connStr)

conn.Open()

conn.ChangeDatabase("psdb")

Dim cmd As New MySqlCommand("SELECT actor_pic, filesize, filename FROM actors WHERE actor_name = ?autoid", conn)

Cmd.Parameters.AddWithValue("?autoid", Actor1Box.Text)

Reader = cmd.ExecuteReader

Reader.Read()

'data is in memory

FileSize = Reader.GetUInt32(Reader.GetOrdinal("filesize"))

rawData = New Byte(FileSize) {}

'get the bytes and filesize

Reader.GetBytes(Reader.GetOrdinal("actor_pic"), 0, rawData, 0, FileSize)

Dim ad As New System.IO.MemoryStream(100000)

' Dim bm As New Bitmap

ad.Write(rawData, 0, FileSize)

Dim im As Image = Image.FromStream(ad) * "error occurs here" (see below)

Actor1Pic.Image = im

Reader.Close()

conn.Close()

conn.Dispose()

ad.Dispose()

I get the error "parameter not valid" in the area noted. FYI If anyone even has some better (working) code examples than this that I can plug in versus debugging this mess that would be great too.

解决方案

Well since getting no help i bashed away at the problem and got it to work finally. Here is my working code.

SAVE TO MySQL out of Picturebox (pbPicture)

Dim filename As String = txtName.Text + ".jpg"

Dim FileSize As UInt32

conn.Close()

Dim mstream As New System.IO.MemoryStream()

PbPicture.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)

Dim arrImage() As Byte = mstream.GetBuffer()

FileSize = mstream.Length

Dim sqlcmd As New MySqlCommand

Dim sql As String

mstream.Close()

sql = "insert into [your table] (picture, filename, filesize)

VALUES(@File, @FileName, @FileSize)"

Try

conn.Open()

With sqlcmd

.CommandText = sql

.Connection = conn

.Parameters.AddWithValue("@FileName", filename)

.Parameters.AddWithValue("@FileSize", FileSize)

.Parameters.AddWithValue("@File", arrImage)

.ExecuteNonQuery()

End With

Catch ex As Exception

MsgBox(ex.Message)

Finally

conn.Close()

End Try

LOAD from MySQL db Back to Picturebox

Dim adapter As New MySqlDataAdapter

adapter.SelectCommand = Cmd

data = New DataTable

adapter = New MySqlDataAdapter("select picture from [yourtable]", conn)

NOTE!! can only put once picture in picturebox so obvoiusly this query can only return one record for you

commandbuild = New MySqlCommandBuilder(adapter)

adapter.Fill(data)

Dim lb() As Byte = data.Rows(0).Item("picture")

Dim lstr As New System.IO.MemoryStream(lb)

PbPicture.Image = Image.FromStream(lstr)

PbPicture.SizeMode = PictureBoxSizeMode.StretchImage

lstr.Close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值