您好,我在较早之前发布了此内容,并获得了一些帮助,但仍然没有有效的解决方案。由于最后的问与答,我确定我的“保存到数据库”代码以及“检索到图片”代码有问题。即使我手动将图片保存在数据库中,它也不会恢复。这是我从网络上的3或4个示例中修补而成的代码。理想情况下,如果有人拥有一些已知的良好代码,并且可以指导我这样做,那将是最好的。
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)
![在此处输入图片描述] [1]
‘*使用以下代码检索到图片框:
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()
好吧,既然没有帮助,我就解决了这个问题,终于使它起作用了。这是我的工作代码。
从Picturebox中保存到MySQL(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
从MySQL db加载回到Picturebox
Dim adapter As New MySqlDataAdapter adapter.SelectCommand = Cmd data = New DataTable adapter = New MySqlDataAdapter("select picture from [yourtable]", conn)
NOTE!! can only put one picture in picturebox so obviously 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()