我正在开发一个需要上载和检索数据库中图像而不是图像路径的应用程序。当我在数据库中上传图像时,它将图像存储在13B中,当我检索图像时,MySQL会显示错误,提示“参数无效” 。请帮我一个星期来解决这个问题。
用于将图像上传到数据库的代码
Image temp = new Bitmap(path);
MemoryStream strm = new MemoryStream();
temp.Save(strm, System.Drawing.Imaging.ImageFormat.Jpeg);
ImageByteArray = strm.ToArray();
string query = "INSERT INTO image_table(image_id,image_data)VALUES(2,'"+ImageByteArray+"')";
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteNonQuery();
从数据库下载图像的代码
string Qqquery = "SELECT image_data from image_table WHERE image_id=1";
MySqlCommand cmd = new MySqlCommand(Qqquery, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
Byte[] bindata = (Byte[])dataReader["image_data"];
MemoryStream ms = new MemoryStream(bindata);
Image dimg = Image.FromStream(ms);
pictureBox2.Image = dimg;
}
dataReader.Close();