如何在VB.NET(Windows窗体)中将PictureBox图像保存到SQL

发布于 2025-01-01 08:37:10 字数 1411 浏览 0 评论 0原文

我需要在其中保存一个表单,用户浏览图像&将其设置为 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 到已知托管提供程序本机类型的映射。”

I need to save a form in it user browse image & set it to a PictureBox But on another button I need save that image to SQL Server .I have a stored procedure with Insert Command (with Image Datatype)

Browser Image from Desktop, PictureBox Code :-

   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

Save Button Code

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

But When I run the form it gives me error "No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2025-01-08 08:37:10

试试这个:(将 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

try this: (Change MySqlConnection to SQLConnection because i'm using MySQL in my example)

    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

then use it like this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文