【问题标题】:How to save PictureBox Image to SQL in VB.NET ( Windows Form )如何在 VB.NET(Windows 窗体)中将 PictureBox 图像保存到 SQL
【发布时间】:2012-02-02 14:25:39
【问题描述】:

我需要在其中保存一个表单,用户浏览图像并将其设置为 PictureBox 但在另一个按钮上我需要将该图像保存到 SQL Server。我有一个带有插入命令的存储过程(带有图像数据类型)

来自桌面的浏览器图像,PictureBox 代码:-

   Public Sub SelectImage()

        With OpenFileDialog1
            '.InitialDirectory = "C:\"
            .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
            .FilterIndex = 4
        End With

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            PictureBox1.BorderStyle = BorderStyle.Fixed3D

        End If
    End Sub

保存按钮代码

Public Sub Insert_Update_Personal()
        Dim UploadImage As Bitmap = PictureBox1.Image

        Dim ds As DataSet = New DataSet()
        Dim cmd As SqlCommand = New SqlCommand("sp_Insert_Update_Personal", con)
        con.Open()
        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.AddWithValue("@childrenage", TextBox10.Text)
        cmd.Parameters.AddWithValue("@picture", UploadImage)
        cmd.Parameters.AddWithValue("@hrcomments", TextBox5.Text)

        cmd.ExecuteNonQuery()
        con.Close()
        cmd.Dispose()
    End Sub

但是当我运行表单时,它给了我错误“不存在从对象类型 System.Drawing.Bitmap 到已知托管提供程序本机类型的映射。”

【问题讨论】:

  • 您需要先将图像转换为字节数组,然后再将其推入数据库。
  • 另外,从原始图像,而不是从图片框。

标签: .net vb.net winforms picturebox


【解决方案1】:

试试这个:(将 MySqlConnection 更改为 SQLConnection,因为我在我的示例中使用的是 MySQL

    Public Shared Function ByteToImage(ByVal blob() As Byte) As Bitmap
        Dim mStream As New MemoryStream
        Dim pData() As Byte = DirectCast(blob, Byte())
        mStream.Write(pData, 0, Convert.ToInt32(pData.Length))
        Dim bm As Bitmap = New Bitmap(mStream, False)
        mStream.Dispose()
        Return bm
    End Function

然后像这样使用它:

Private Function InsertImage(ByVal ImagePath As String, ByVal oIDNum As String) As Boolean
    Dim iPhoto() As Byte = jwImage.FileImageToByte(ImagePath)
    Dim xBool As Boolean = False
    Using xConn As New MySqlConnection(ConnectionClass.ConnectionString)
        Try
            Dim xComm As New MySqlCommand
            With xComm
                .CommandText = "InsertImage"
                .Connection = xConn
                .CommandType = CommandType.StoredProcedure

                .Parameters.AddWithValue("xID", oIDNum)
                .Parameters.AddWithValue("xImage", iPhoto)
            End With

            xConn.Open()
            xComm.ExecuteNonQuery()
            xComm.Dispose()
            xBool = True
        Catch ex As MySqlException
            MessageBox.Show(ex.Message, "MySQL Error: " & ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            xBool = False
        End Try
    End Using

    Return xBool
End Function

【讨论】:

  • 您好,谢谢,只是想知道什么是“jwImage”,它是 PictureBox ID 吗?
  • 你能解释一下这行 Dim iPhoto() As Byte = jwImage.FileImageToByte(ImagePath) 还有什么时候我会调用这个函数“ByteToImage”
  • @Pankaj jwImage 是一个类的名称。 FileImageToByte 顾名思义,就是把一个 Image 转换成 Byte Array。
猜你喜欢
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 2012-08-17
相关资源
最近更新 更多