美好的一天!, 我试图显示/调用从特定datagridrow到图片框的图像,但是我找不到适合它的任何代码。
我找到了一个简单的代码,但对我却不起作用,这是代码的链接:http://1bestcsharp.blogspot.com/2016/03/vb-net-datagridview-image-to-picturebox.html
我从catch中得到了这个错误:
这部分代码给我带来麻烦。
img = selectedRow.Cells(0).Value
这是完整的代码
Private Sub Student_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Student.CellClick
Dim index As Integer
Dim ms As New MemoryStream
Dim img As Bitmap
index = e.RowIndex
Dim selectedRow As DataGridViewRow
selectedRow = Student.Rows(index)
Try
img = selectedRow.Cells(0).Value
img.Save(ms, ImageFormat.Jpeg)
Pic1.Image = Image.FromStream(ms)
Catch ex As Exception
MsgBox(ex.Message)
End Try
idnum.Text = selectedRow.Cells(1).Value.ToString()
fname.Text = selectedRow.Cells(2).Value.ToString()
midname.Text = selectedRow.Cells(3).Value.ToString()
lname.Text = selectedRow.Cells(4).Value.ToString()
course.Text = selectedRow.Cells(5).Value.ToString()
address.Text = selectedRow.Cells(6).Value.ToString()
Contactnum.Text = selectedRow.Cells(7).Value.ToString()
rfid.Text = selectedRow.Cells(8).Value.ToString()
End Sub
答案 0 :(得分:0)
也许您可以使用Item而不是Cells进行尝试。所以你只要写
Dim selectedRow As DataGridViewRow
selectedRow = Student.Rows(index)
img = selectedRow.Item(0)
希望这会有所帮助。否则,请问任何问题,因为在过去几天中我做了非常类似的事情。
答案 1 :(得分:0)
Function Byte2Image(ByVal ByteArr() As Byte) As Bitmap
Dim ImageStream As MemoryStream
Try
If ByteArr.GetUpperBound(0) > 0 Then
ImageStream = New MemoryStream(ByteArr)
ImageStream.Seek(0, SeekOrigin.Begin)
Dim bmp As Bitmap = New Bitmap(ImageStream)
Return bmp
Else
Return Nothing
End If
Catch ex As Exception
Return Nothing
End Try
End Function
使用:
Dim img = Byte2Image(selectedRow.Cells(0).Value)
If img IsNot Nothing
Pic1.Image = img
End If