如何将 DataGridView 中的图像显示到 PictureBox?
我正在尝试在我的 DataGridView 中的 PictureBox 中显示图像.
我使用 SQL Server 作为数据库.
I'm trying to show an image in my DataGridView in a PictureBox.
I'm using SQL Server as database.
Me.PictureBox2.Image = DataGridView2.Item(10, i).Value
我想知道我使用的是什么代码.
I'm wondering what code I am using.
这是错误说:
Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.
推荐答案
您需要将 Blob 字段 Byte 数组(现在是 DataGridView
单元格值)转换为 Image
对象.
You need to convert the Blob field Byte array (now a DataGridView
cell Value) to an Image
object.
一个 MemoryStream 可用于收集Byte 数组并成为 Stream 源"nofollow noreferrer">Image.FromStream() 方法.
A MemoryStream can be used to collect the Byte array and become the Stream
source for the Image.FromStream() method.
If DataGridView2(10, 1).Value Is Nothing Then Return
Using ms As MemoryStream = New MemoryStream(CType(DataGridView2(10, i).Value, Byte()))
PictureBox2.Image?.Dispose()
PictureBox2.Image = Image.FromStream(ms)
End Using
相关文章