gpt4 book ai didi

mysql - 如何在 VB.NET 中从 MySQL 数据库检索图像到 PictureBox

转载 作者:行者123 更新时间:2023-11-29 23:36:08 25 4
gpt4 key购买 nike

我正在尝试将图像获取到另一种形式,数据库中的所有文本记录都工作正常,但是当我添加图像时,出现错误,提示无法转换类型为“System.Byte”的对象[]' 输入'System.Drawing.Image'。,我错过了什么?

这是我的第一种形式的代码:

Imports MySql.Data.MySqlClient

Public Class Form5

Dim a As OpenFileDialog = New OpenFileDialog
Public dc As Integer
Public ccfname As String
Public ccmname As String
Public cclname As String
Public ccpos As String
Public ccparty As String
Public photo As Image

Dim con As New MySqlConnection

Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New MySqlConnection
If con.State = ConnectionState.Closed Then
con.ConnectionString = "server=localhost;user id=root;database=db;password=root"
con.Open()
End If
LoadPeople()
End Sub

Public Sub LoadPeople()
Dim sConnection As New MySqlConnection
sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
sConnection.Open()
Dim sqlQuery As String = "SELECT * FROM candidate WHERE cfname<>'Select a Candidate' AND candidacy='Filed'"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim TABLE As New DataTable
Dim i As Integer


With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
End With

With sqlAdapter
.SelectCommand = sqlCommand
.Fill(TABLE)
End With

LvPeople.Items.Clear()

For i = 0 To TABLE.Rows.Count - 1
With LvPeople
.Items.Add(TABLE.Rows(i)("idn"))
With .Items(.Items.Count - 1).SubItems
.Add(AddFieldValue(TABLE.Rows(i), ("cpos")))
.Add(AddFieldValue(TABLE.Rows(i), ("cfname")))
.Add(AddFieldValue(TABLE.Rows(i), ("cmname")))
.Add(AddFieldValue(TABLE.Rows(i), ("clname")))
.Add(AddFieldValue(TABLE.Rows(i), ("cparty")))
End With
End With
Next
End Sub

Private Function AddFieldValue(ByVal row As DataRow, ByVal fieldName As String) As String
If Not DBNull.Value.Equals(row.Item(fieldName)) Then
Return CStr(row.Item(fieldName))
Else
Return Nothing
End If
End Function

Private Sub lvPeople_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LvPeople.MouseClick
dc = LvPeople.SelectedItems(0).Text
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If dc = Nothing Then
MsgBox("Please choose a record to view.", MsgBoxStyle.Exclamation)
Else

Dim sqlCommand As New MySqlCommand
con.ConnectionString = "server = localhost; user id = root; database = db; password = root"
sqlCommand.Connection = con
con.Open()
Dim sqlQuery As String = "SELECT * FROM candidate WHERE idn = '" & LvPeople.SelectedItems(0).Text & "'"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlTabble As New DataTable

With sqlCommand
.CommandText = sqlQuery
.Connection = con
.ExecuteNonQuery()
End With

With sqlAdapter
.SelectCommand = sqlCommand
.Fill(sqlTabble)
End With
Form25.dc = LvPeople.SelectedItems(0).Text
Form25.ccfname = sqlTabble.Rows(0)("cfname")
Form25.ccmname = sqlTabble.Rows(0)("cmname")
Form25.cclname = sqlTabble.Rows(0)("clname")
Form25.ccpos = sqlTabble.Rows(0)("cpos")
Form25.ccparty = sqlTabble.Rows(0)("cparty")
Form25.photo = sqlTabble.Rows(0)("photo")
Form25.ShowDialog()
con.Close()

dc = Nothing
End If
End Sub

Private Sub LvPeople_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LvPeople.SelectedIndexChanged
Dim connstring As String = "server = localhost; user id = root; database = db; password = root"
Dim Sql As String = "SELECT * FROM candidate WHERE idn='" & LvPeople.SelectedItems(0).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("photo"), Byte())
Using ms As New IO.MemoryStream(imagebytes)
PictureBox1.Image = Image.FromStream(ms)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Using
conn.Close()
End Sub

这是我试图获取图像的第二个表单。

Imports MySql.Data.MySqlClient
Public Class Form25

Dim a As OpenFileDialog = New OpenFileDialog
Public sConnection As New MySqlConnection
Friend dc As Integer
Friend ccfname As String
Friend ccmname As String
Friend cclname As String
Friend ccpos As String
Friend ccparty As String
Friend photo As Image

Private Sub Form25_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
sConnection.Open()
End If
Dim connstring As String = "server = localhost; user id = root; database = db; password = root"
Dim Sql As String = "SELECT * FROM candidate WHERE idn='" & Form5.LvPeople.SelectedItems(0).Text & "'"
Dim conn As New MySqlConnection(connstring)
Dim cmd As New MySqlCommand(Sql, conn)
TextBox2.Text = ccfname
TextBox3.Text = ccmname
TextBox4.Text = cclname
ComboBox1.Text = ccpos
TextBox1.Text = ccparty
PictureBox1.Image = photo
End Sub
End Class

这里的错误点:

Form25.dc = LvPeople.SelectedItems(0).Text
Form25.ccfname = sqlTabble.Rows(0)("cfname")
Form25.ccmname = sqlTabble.Rows(0)("cmname")
Form25.cclname = sqlTabble.Rows(0)("clname")
Form25.ccpos = sqlTabble.Rows(0)("cpos")
Form25.ccparty = sqlTabble.Rows(0)("cparty")
Form25.photo = sqlTabble.Rows(0)("photo") <<--------------HERE------|
Form25.ShowDialog()
con.Close()

这是第一种形式。

最佳答案

这就是我用来做类似事情的方法。这将让你使用:

Form25.photo = Byte2Image(sqlTabble.Rows(0)("photo"))

您可能需要进行一些显式转换。

Public Shared Function Byte2Image(ByVal byteArr() As Byte) As Drawing.Image

Using ImageStream As New MemoryStream(byteArr)
Dim newImage As Drawing.Image
Try
If byteArr.GetUpperBound(0) > 0 Then
newImage = System.Drawing.Image.FromStream(ImageStream)
Else
newImage = Nothing
End If
Catch ex As Exception
newImage = Nothing
End Try
Return newImage
End Using

End Function

关于mysql - 如何在 VB.NET 中从 MySQL 数据库检索图像到 PictureBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26367297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com