如何将 DataGridView 中的图像显示到 PictureBox? [英] How to show an image from a DataGridView to a PictureBox?

查看:51
本文介绍了如何将 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆