如何将 PictureBox 中的图像保存到我的数据库中?

Posted

技术标签:

【中文标题】如何将 PictureBox 中的图像保存到我的数据库中?【英文标题】:How to save an Image from a PictureBox to my database? 【发布时间】:2021-02-16 12:56:50 【问题描述】:
private Sub Button1_Click(ByVal sender As System. Object, ByVal e As System.EventArgs) Handles Button1.Click
    If conn.State = ConnectionState.Open Then
        conn.Close()
    End If
    If Trim(txtcode.Text) = "" Then
        MsgBox("Please enter the unique Member code")
        txtcode.Focus()
        Exit Sub
    ElseIf IsNumeric(txtName_Surname.Text) Then
        MsgBox("Please replace numbers with text", MessageBoxIcon.Stop)
        txtName_Surname.Focus()
        Exit Sub
    Else
        Try
            Dim strinsert As String
            Dim cmdinsert As OleDb.OleDbCommand
            Dim Passport As String = Application.ExecutablePath
            strinsert = "insert into book_mast (Mem_code,Name_Surname,Date_of_Birth,Gender,Telephone_number,Age,Status,Date_of_Baptism,House_No,Occupation,Education,Office_Name,Tel,Office_No,Tithe_per_month,Total_tithe_per_year,Born_Again,BA_Time,Baptizism,Baptizism_where,Join_RCCG,Join_Where,Baptizism_Ghost,BPG_When,Tongues,Tongues_When, Passport) values(@txtcode, @txtName_Surname, @d1, @txtgen, @txtTelephone_number, @txtage, @CbBStatus, @d2, @txthno, @TxtOccupation, @CbBEducation, @TxtOffName, @TxtTel, @txtOno, @txtTithe_per_month, @txtTotal_tithe_per_year, @TxtBornAgain, @TxtBornwhen, @CbBbaptizism, @TxtBaptizedwhere, @TxtJoinRCCG, @TxtJoinWhere, @TxtHolyGhost, @TxtGhostWhen, @TxtTongues, @TxtTonguesWhen, @Passport)"
            cmdinsert = New OleDb.OleDbCommand(strinsert, conn)
            conn.Open()
            cmdinsert.ExecuteNonQuery()
            MsgBox("Record Has been added Successfully")
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
            clearall()
            fillgridmem()
        End Try
    End If
End Sub

【问题讨论】:

你能澄清一下问题到底是什么,以便我可以帮助并尝试编辑问题并将 vb 代码包装在“”中,如果代码出现任何错误。 请重新格式化,使其更易于阅读 我看不到您在哪里为参数赋值。 我没有看到任何努力来保存Image 那么实际问题是什么?如果您在网上搜索如何将Image 保存到数据库中,那么您会发现大量信息,因此至少没有理由不尝试。 不要;将其保存到磁盘文件系统并将文件路径放入数据库中 【参考方案1】:

我创建了一个小程序来帮助您理解。在这种情况下,我在 access 中创建了一个数据库,但您可以使用您拥有的数据库。

Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\Data.mdb"

Dim imgName As String
Dim daImage As OleDbDataAdapter
Dim dsImage As DataSet

Private Sub FillCombo()
    Try
        Dim CN As New OleDbConnection(cnString)

        CN.Open()
        daImage = New OleDbDataAdapter()
        daImage.SelectCommand = New OleDbCommand("SELECT * FROM images", CN)
        dsImage = New DataSet("dsImage")

        daImage.Fill(dsImage)

        Dim dtable As DataTable

        dtable = dsImage.Tables(0)
        cboImageID.Items.Clear()

        For Each drow As DataRow In dtable.Rows
            cboImageID.Items.Add(drow(0).ToString())
            cboImageID.SelectedIndex = 0
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
    Try
        Dim dlgImage As FileDialog = New OpenFileDialog()

        dlgImage.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"

        If dlgImage.ShowDialog() = DialogResult.OK Then
            imgName = dlgImage.FileName

            Dim newimg As New Bitmap(imgName)

            imgSave.SizeMode = PictureBoxSizeMode.StretchImage
            imgSave.Image = DirectCast(newimg, Image)
        End If

        dlgImage = Nothing
    Catch ae As System.ArgumentException
        imgName = " "

        MessageBox.Show(ae.Message.ToString())
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        If imgName <> "" Then
            Dim fs As FileStream

            fs = New FileStream(imgName, FileMode.Open, FileAccess.Read)

            Dim picByte As Byte() = New Byte(fs.Length - 1) 

            fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))

            fs.Close()

            Dim CN As New OleDbConnection(cnString)

            CN.Open()

            Dim strSQL As String

            strSQL = "INSERT INTO Images([Image]) values (" & " @Img)"

            Dim imgParam As New OleDbParameter()

            imgParam.OleDbType = OleDbType.Binary
            imgParam.ParameterName = "Img"
            imgParam.Value = picByte

            Dim cmd As New OleDbCommand(strSQL, CN)

            cmd.Parameters.Add(imgParam)
            cmd.ExecuteNonQuery()

            MessageBox.Show("Image successfully saved.")

            cmd.Dispose()
            CN.Close()
            CN.Dispose()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

    FillCombo()
End Sub

Private Sub btnRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click
    Dim dataTable As DataTable = dsImage.Tables(0)

    If imgRetrieve.Image IsNot Nothing Then

        imgRetrieve.Image.Dispose()
    End If

    Dim fsImage As New FileStream("image.jpg", FileMode.Create)

    For Each dataRow As DataRow In dataTable.Rows
        If dataRow(0).ToString() = cboImageID.SelectedItem.ToString() Then

            Dim blob As Byte() = DirectCast(dataRow(1), Byte())

            fsImage.Write(blob, 0, blob.Length)
            fsImage.Close()
            fsImage = Nothing

            imgRetrieve.Image = Image.FromFile("image.jpg")
            imgRetrieve.SizeMode = PictureBoxSizeMode.StretchImage
            imgRetrieve.Refresh()
        End If
    Next
End Sub

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    FillCombo()
End Sub

如果这不能解决您的问题或出现任何错误,请告诉我,我会调查。

【讨论】:

我有这个代码,但如何个性化它是我的挑战,我可以和你分享我的代码吗? 请务必分享您的代码,以便我进行更改。

以上是关于如何将 PictureBox 中的图像保存到我的数据库中?的主要内容,如果未能解决你的问题,请参考以下文章

如何将二进制图像保存为任何格式到我的电脑上

如何将 google image map API 图片保存到我的服务器

c#picturebox指向文件系统

保存图片框图片

如何加载保存到我的标记 ID 的图像并显示在该特定标记的自定义信息窗口中?

我应该如何从屏幕空间坐标转换为 WinForms PictureBox 中的图像空间坐标?