c# - 试图在 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# - redis缓存的单元测试
- snowflake-cloud-data-platform - 生成嵌套 JSON(反向横向展平)
- java - 如何在 Java 中实现 UML 类图
- python - 如果连续整数形式的子列表排序为整数的非重复列表,是否有更有效的方法来找到获取列表的解决方案?
- android - 适合我的用例的最佳 Android 范围存储选项
- rubygems - jekyll server --livereload 抛出错误
- java - 如何删除空格“U+3164”?
- swift - 使用 UserDefault 将 UIKit 故事板数据传递给小部件
- python - 如何从这个 json 中过滤作者 ID?
- mapping - 提取要根据 id 呈现的数组中的项目/道具列表