c# - 如何从 blob 类型的 MySql 数据库中获取图像?
问题描述
我需要在图片框中显示存储在数据库中的 blob 类型的图像。在我的程序中,我通过阅读器接收查询数据并将它们加载到对象中。然后我使用对象的属性在图片框中显示图像。使用我当前的代码,图像显示为空白。
查询运行良好,问题在于将数据库的 blob 类型转换为 Image 类型。
MySqlCommand comando = new MySqlCommand(consulta, Conexion.Con);
Conexion.AbrirConexion(); //open connection
MySqlDataReader reader = comando.ExecuteReader();
Usuario usuario = new Usuario();
if (reader.HasRows)
{
while (reader.Read())
{
byte[] num = (byte[])reader[4];
MemoryStream ms = new MemoryStream(num);
Image returnImage = Image.FromStream(ms);
Image image = returnImage;
usuario.Nombre = reader.GetString(1);
usuario.Nickname = reader.GetString(2);
usuario.Contrasenya = reader.GetString(3);
usuario.Imagen = image;
}
}
Conexion.CerrarConexion(); //close connection
解决方案
推荐阅读
- javascript - 我想用表格中的日期控制时间
- javascript - React,从 index.html 打开
- ios - Swift Popup 如何将值传递给 PopupViewController
- php - json 中出现意外的“s”时
- sockets - Nginx 和 Webpack-Dev-Server 出现错误“net::ERR_CERT_COMMON_NAME_INVALID”
- firebase - 如何从 itemBuilder 访问查询?
- spring-integration - 对 HttpOutboundGateway 进行轮询
- c++ - 多重声明与未定义
- javascript - 无法让预先编写的 Tizen 应用程序工作
- python - tree.put(key, value) 插入