我试图以BLOB格式检索保存在数据库中的图片 但它给出了一个异常,即MemoryStream的参数在此行
中无效pictureBox1.Image = System.Drawing.Image.FromStream(ms);
这是完整的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
namespace InsertImage2
{
public partial class Form1 : Form
{
MySqlConnection myConn = new MySqlConnection("datasource=localhost;port=****;username=****;password=****;");
MySqlCommand cmd;
MySqlDataAdapter da;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string query = "select * from images_database.images where id = '"+textBox1.Text+"';";
cmd = new MySqlCommand(query, myConn);
da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
byte[] img = (byte[])dt.Rows[0][1];
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
da.Dispose();
}
}
}