How to show an image from a DataGridView to a PictureBox?(如何将 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
这篇关于如何将 DataGridView 中的图像显示到 PictureBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 DataGridView 中的图像显示到 PictureBox?


基础教程推荐
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01