gpt4 book ai didi

mysql - 将图像存储到数据库 blob;从数据库检索到 Picturebox

转载 作者:行者123 更新时间:2023-11-29 18:10:09 26 4
gpt4 key购买 nike

您好,我之前发布了此内容并获得了一些帮助,但仍然没有有效的解决方案。感谢上一个问答,我确定我的“保存到数据库”代码以及“检索到图片”代码有问题。即使我手动将图片保存在数据库中,它仍然无法检索。这是我根据网络上的 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 数据库加载回到 Picturebox

   Dim adapter As New MySqlDataAdapter
adapter.SelectCommand = Cmd

data = New DataTable

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

注意!!只能在图片框中放入一张图片,因此显然此查询只能为您返回一条记录

    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()

关于mysql - 将图像存储到数据库 blob;从数据库检索到 Picturebox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47477104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com