使用c#以BLOB格式显示存储在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()); } 

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

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

 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; } 

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

  yourPictureBox.Image = Image.FromStream(ms);