我必须在 MySQL 数据库中存储和恢复图像。为此,我使用了此页面上给出的说明:MySQL Forum,我可以通过以下代码成功存储图像:
保存图片:
void SaveImage()
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
cmd = new MySqlCommand("INSERT INTO " + tableName + " ( Product, Manufacturer, Description, Price, Image) Values ('New_Product', 'New_Manufacturer', 'New_Description', '0', @Image)", conn);
cmd.Parameters.Add(new MySqlParameter("@Image", Convert.ToBase64String(ms.ToArray())));
cmd.ExecuteNonQuery();
}
但我无法从数据库中获取图像:我正在使用以下代码,请检查这里有什么问题:
获取缩略图:
void GetThumbnail()
{
string TN = tableNames[comboBox1.SelectedIndex];
cmd = new MySqlCommand("SELECT Image FROM " + tableName + " WHERE Product = " + ProductTitle(), conn);
object imgObj = cmd.ExecuteScalar();
byte[] b = Convert.FromBase64String(Convert.ToString(imgObj));
MemoryStream ms = new MemoryStream(b);
pictureBox2.Image = Image.FromStream(ms);
}