开发者社区> 问答> 正文

将图像存储到数据库Blob;从数据库检索到Picturebox?mysql

您好,我在较早之前发布了此内容,并获得了一些帮助,但仍然没有有效的解决方案。由于最后的问与答,我确定我的“保存到数据库”代码以及“检索到图片”代码有问题。即使我手动将图片保存在数据库中,它也不会恢复。这是我从网络上的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()

展开
收起
保持可爱mmm 2020-05-17 12:46:49 692 0
1 条回答
写回答
取消 提交回答
  • 好吧,既然没有帮助,我就解决了这个问题,终于使它起作用了。这是我的工作代码。

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

    注意!!只能在Picturebox中放置一张图片,因此很明显,此查询只能为您返回一张记录

    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()来源:stack overflow
    
    2020-05-17 12:56:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载

相关镜像