我正在尝试将图像提取到另一个表单中,来自数据库的所有文本记录都正常工作,但是当我添加图像时,它会显示错误Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.
,我错过了什么?< / p>
这是我的第一个表格的代码:
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()
这是第一种形式。
答案 0 :(得分:0)
这是我用来做类似的事情。 这可以让你使用:
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