首页 > 解决方案 > 在 C# winform 应用程序中将图像从 MySql 加载到 PictureBox2 时参数无效

问题描述

请帮我解决这个问题,因为我已经尝试了多个来源建议的许多解决方案。我有一个pictureBox2,它下面有一个显示按钮。这个想法是,如果我单击显示按钮,JPEG 图像将出现在我的MySql数据库中的图片框2 中。该图像位于名为 Recipy 的表中的 blob 字段中(请原谅我对食谱的拼写错误)。这是我的代码:

MySqlCommand comm;
MySqlDataReader rdr;
MySqlDataAdapter da;

string Query = "select image from chefassist.recipy where name ='" + textBox1.Text + "';";   
MySqlConnection connectToD = new MySqlConnection("server = localhost; database = chefassist; uid 
     = root; pwd = n0clu387;");
comm = new MySqlCommand(Query, connectToD);
da = new MySqlDataAdapter(comm);

try
{
    connectToD.Open();
   
    rdr = comm.ExecuteReader();
     while(rdr.Read())
    {
        //string sName = rdr.GetString("name");
        //textBox1.Text = sName;
        byte[] imgg = (byte[])(rdr["image"]);
        if (imgg == null)
        {
            pictureBox2.Image = null;
        }
        else
        {
            MemoryStream mstream = new MemoryStream(imgg);
            pictureBox2.Image = System.Drawing.Image.FromStream(mstream);
        }
    }

catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }

   

标签: c#mysql

解决方案


这是我将图像保存到 mySql 数据库的代码。我使用 MySqlWorkbench:

try
            {
                OpenFileDialog opn = new OpenFileDialog();
                opn.Filter = "Choose Image(*.jpg; *.png; *.jpeg)|*.jpg; *.png; *.jpeg";
                if (opn.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.Image = Image.FromFile(opn.FileName);
                }
            }
            catch (Exception)
            {

                throw;
            }
            MySqlCommand comm;
            MySqlDataReader rdr;

            //string Query = "UPDATE `chefassist`.`recipy` SET image = '@Img' WHERE name='Herb and Lemon Soup'";
            string Query = "UPDATE `chefassist`.`recipy` image=@img where name='" + this.textBox1.Text + "';";
            MySqlConnection connectToD = new MySqlConnection("server = localhost; database = chefassist; uid = root; pwd = n0clu387;");
            comm = new MySqlCommand(Query, connectToD);
            connectToD.Open();

            try
            {
                MemoryStream ms = new MemoryStream();
                pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                byte[] img = ms.ToArray();
                MySqlCommand cmd;
                //String qry = "UPDATE `chefassist`.`recipy` SET image = '@Img' WHERE name = 'Herb and Lemon Soup'";
                string qry = "UPDATE `chefassist`.`recipy` SET image=@img where name='" + this.textBox1.Text + "';";
                comm = new MySqlCommand(qry, connectToD);
                //comm.Parameters.Add("@Id", MySqlDbType.Int32);
                comm.Parameters.Add("@img", MySqlDbType.MediumBlob);
                //comm.Parameters["@Id"].Value = txtAddress.Text;
                comm.Parameters["@img"].Value = img;
                if (comm.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Data Inserted");
                }
            }
            catch (Exception)
            {
                throw;
            }

这是我保存 ByteArrayToImage 的函数:

public Image ByteArrayToImage(byte[] buffer)
        {

            MemoryStream ms = new MemoryStream(buffer);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

最后,这是我的显示按钮中的当前代码:

MySqlCommand comm;
            
            //MySqlDataReader rdr;
            //MySqlDataAdapter da;

            string Query = "select image from chefassist.recipy where image = @img";

            using (MySqlConnection connectToD = new MySqlConnection("server = localhost; database = chefassist; uid = root; pwd = n0clu387;"))
            {
                using (comm = new MySqlCommand(Query, connectToD))
                {
                    comm.Parameters.AddWithValue("@img",textBox1.Text);
                    connectToD.Close();
                    try
                    {
                        connectToD.Open();
                        var rdr = comm.ExecuteReader();

                        while (rdr.Read())
                        {
                            byte[] imgg = (byte[])(rdr["image"]);
                            
                            if (imgg == null)
                            {
                                pictureBox2.Image = null;
                            }
                            else
                            {
                                //pictureBox2.Image = System.Drawing.Image.FromStream(imgg);
                                pictureBox2.Image = ByteArrayToImage(imgg);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

推荐阅读