如何将图像从数据库添加到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数据库中的图像,我使用此代码将图像保存到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.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 cmd As New SqlCommand(\"INSERT INTO projectimages (imagename,imagedate,csvprojectref) VALUES (\'\" + imageFilename + \"\',@BLOBData,CHECKSUM(\'\" + textboxFileRef.Text + \"\'))\",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\',\'\" + img + \"\',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
    ,        按照Microsoft使用MemoryStream对象。     ,        
Dim img As Byte() = DirectCast(cmd.ExecuteScalar(),Byte())
Dim ms as MemoryStream = New MemoryStream(img)
pictureBox.Image = Image.FromStream(ms)
假设imagedate字段为
blob
字段。     ,        这是我的代码,用于在vb.net中将blob文件显示到picturebox。希望能帮助到你
Dim connstring As String = \"Database=pmk;data source=localhost;user id=root;password=\"
Dim Sql As String = \"select * from mastermahasiswa where Nim=\'\" & TextBox1.Text & \"\'\"
Dim conn As New MySqlConnection(connstring)
Dim cmd As New MySqlCommand(Sql,conn)
Dim dr As MySqlDataReader = Nothing
conn.Open()
dr = cmd.ExecuteReader()
dr.Read()
Dim imagebytes As Byte() = CType(dr(\"Foto\"),Byte())
Using ms As New IO.MemoryStream(imagebytes)
    PictureBox1.Image = Image.FromStream(ms)
    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Using
conn.Close()
    ,        我也收到参数无效错误。为了解决此问题,我在窗体中添加了一个图片框,该框用于加载要复制到SQL Server的图像作为背景图像。该语句似乎很关键:
PictureBox1.BackgroundImage.Save(ms,PictureBox1.BackgroundImage.RawFormat)
这会使实际上传花费更长的时间,但是我不再遇到参数错误...我认为是因为图片现在实际上是有效的图片。
        SSScmd.CommandText = \"INSERT INTO PreviewSlideshow (Name,Photo) VALUES (@name,@photo)\"

        Dim p As New SqlParameter(\"@photo\",SqlDbType.Image)

        For Each CurFile As IO.FileInfo In New IO.DirectoryInfo(sSourcePath).GetFiles

            If CurFile.Name = \"Thumbs.db\" Then
                \'skip
            Else

                If CurFile.Extension = \"jpg\" Or CurFile.Extension = \"png\" Then

                    \'show the image name on the form
                    ImageName.Text = CurFile.Name

                    SSScmd.Parameters.AddWithValue(\"@name\",ImageName.Text)

                    Dim ms As New MemoryStream()

                    PictureBox1.BackgroundImage = Image.FromFile(sSourcePath & CurFile.Name)

                    PictureBox1.BackgroundImageLayout = ImageLayout.Stretch

                    PictureBox1.BackgroundImage.Save(ms,PictureBox1.BackgroundImage.RawFormat)

                    Dim data As Byte() = ms.GetBuffer()

                    p.Value = data

                    SSScmd.Parameters.Add(p)

                    SSScmd.ExecuteNonQuery()

                    SSScmd.Parameters.Clear()



                End If
            End If

        Next
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...