如何从WindowsForms从MySQL获取图像

时间:2016-12-10 22:26:44

标签: c# mysql

我正在尝试将位图图像(jpeg,png,jpg)从WinForms PictureBox插入到MYSQL DB BLOB属性,并再次将其读取到该pictureBox。 Insert执行OK:

user.Img = pictureBoxFoto.Image;

sqlCommand.CommandText = "INSERT INTO`gclinic`.`user`(`Img`)VALUES (@img);";
sqlCommand.Parameters.Add(new MySqlParameter("@img", user.Img));

if (sqlCommand.ExecuteNonQuery() != 1)
{
 throw new InvalidProgramException("Erro: SQLuser - add() Throw() - mysql ");
}

问题似乎是从MYSQL上阅读:

    if (!reader.IsDBNull(reader.GetOrdinal("Img")))
    {
        Byte[] byteBLOBData = new Byte[0];
        byteBLOBData = (Byte[])(reader["Img"]);
        MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
        user.Img = Image.FromStream(stmBLOBData);
    }

抛出异常:' System.ArgumentException'在System.Drawing.dll"参数无效"异常加载System.Drawing.Image

1 个答案:

答案 0 :(得分:0)

将此添加到您的代码中。

byteBLOBData[0]=new Byte();