首页 > 解决方案 > 试图在 C# 中检索 Mysql 数据库中的图像,但 mysql 显示错误“参数无效”

问题描述

我正在开发一个应用程序,我需要在数据库中上传和检索图像,而不是图像路径。当我在数据库中上传图像时,它会将其存储在 13B 中,当我检索它时,MySQL 会显示错误"Parameter is not valid"。请帮助我从一周开始就陷入这个问题。

上传图片到数据库的代码

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();

标签: c#mysqldatabaseimagewinforms

解决方案


推荐阅读