vb.net – 如何将图像从数据库添加到PictureBox?

前端之家收集整理的这篇文章主要介绍了vb.net – 如何将图像从数据库添加到PictureBox?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用它来从数据库获取图像字节
cmd.CommandText = "select imagedate from projectimages where imagename = '" + _
    ListBox1.Text + "' and CSVprojectref=checksum('" + textBoxFileRef.Text + "')"

Dim img As Object = cmd.ExecuteScalar()

现在我该如何将它添加到PictureBox.image.我在检索图像并在PictureBox显示它时遇到了很多麻烦.

数据类型是sql数据库中的Image,我使用此代码将图像保存到db

Dim ms As New IO.MemoryStream

        If imageFilename.Contains("jpeg") Or imageFilename.Contains("jpg") Then
            imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg)

        End If
        If imageFilename.Contains("png") Then
            imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Png)
        End If
        If imageFilename.Contains("gif") Then
            imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Gif)
        End If
        If imageFilename.Contains("bmp") Then
            imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp)
        End If

        Dim bytes() As Byte = ms.ToArray
        Dim img As String = Convert.ToBase64String(bytes)


        Dim cmd As New OleDb.OleDbCommand("insert projectimages values('" + imageNameTemp + "','" + img + "',CHECKSUM('" + textBoxFileRef.Text + "'))",con)
        cmd.ExecuteNonQuery()
经过5-6小时的搜索论坛和博客以及我喜欢的一切…将图像保存到数据库

1-数据类型应该是数据库中的图像

现在在将图像存储到sql数据库添加代码

OpenFileDialog1.ShowDialog()
    imageFilename = OpenFileDialog1.FileName
    Dim imageUpload As Image
    imageUpload = Image.FromFile(OpenFileDialog1.FileName)



    If imageFilename <> "" Then

        Dim imageNameTemp As String

        imageNameTemp = imageFilename

        While (imageNameTemp.Contains("\"))


            imageNameTemp = imageNameTemp.Remove(0,imageNameTemp.IndexOf("\") + 1)
        End While

        Dim ms As New IO.MemoryStream

        If imageFilename.Contains("jpeg") Or imageFilename.Contains("jpg") Then
            imageUpload.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp)
        End If

        'Dim cmd As New sqlCommand("INSERT INTO projectimages (imagename,imagedate,csvprojectref) VALUES ('" + imageFilename + "',@BLOBData,con)

        Dim b() As Byte = ms.ToArray()

        Dim cmd As New sqlCommand("INSERT INTO projectimages (imagename,csvprojectref) VALUES ('" + imageNameTemp + "',con)

        cmd.Parameters.Add("@BLOBData",sqlDbType.Image,b.Length).Value = b
        '    Dim cmd As New sqlCommand("insert projectimages(imagename,csvprojectref) values('imagma',con)

        cmd.ExecuteNonQuery()

        '  cmdTemp.Parameters.Add("@photo",b.Length).Value = b

    End If

并且当检索要插入图片框的数据时使用此代码

cmd.CommandText = "select imagedate from projectimages where imagename = '" +      ListBox1.Text + "' and CSVprojectref=checksum('" + textBoxFileRef.Text + "')"


        cmd.Connection = con
        Dim da As New sqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds,"projectimages")
        Dim c As Integer = ds.Tables(0).Rows.Count
        If c > 0 Then
            Dim bytBLOBData() As Byte = _
                ds.Tables(0).Rows(c - 1)("imagedate")
            Dim stmBLOBData As New MemoryStream(bytBLOBData)
            PictureBox1.Image = Image.FromStream(stmBLOBData)
        End If

猜你在找的VB相关文章