首页 > 解决方案 > 如何从 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

标签: c#mysql

解决方案


推荐阅读