使用c#以BLOB格式显示存储在MySql数据库中的图像

时间:2012-08-26 14:52:53

标签: c# mysql

使用以下功能在db

中存储图像
 void SaveImage(byte[] image)
        {
            MySqlConnection con = new MySqlConnection(db);//new connection is made
            con.Open();

                string cmdText = "INSERT INTO Picture(RoomNo ,pic )VALUES ('" + RoomNo.Text + "',?Image)";
                MySqlCommand cmd = new MySqlCommand(cmdText, con);
                cmd.Parameters.Add("?Image", image);
                cmd.ExecuteNonQuery();

        }

在主要的am调用函数中

 using (var ms = new MemoryStream())
            {
                picbox.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                SaveImage(ms.ToArray());
            }

我不知道如何在一个图片框中显示它..任何人都可以帮助我吗???

2 个答案:

答案 0 :(得分:4)

您使用的是Windows窗体吗?并且您必须将字节数组转换为图像以在图片框中显示它。

public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

你是如何将Image转换为字节数组的?我希望那个问题不存在。您可以使用:

  private byte[] ImageToByteArray(string ImageFile)
    {
        FileStream stream = new FileStream(
              ImageFile, FileMode.Open, FileAccess.Read);
        BinaryReader reader = new BinaryReader(stream);

        // Convert image to byte array.
        byte[] photo = reader.ReadBytes((int)stream.Length);

        return photo;
    }

答案 1 :(得分:0)

使用Image方法(FromStream)设置图像图片框

  yourPictureBox.Image = Image.FromStream(ms);