参数无效

xxslljrj  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(418)

我正在尝试将mysql数据库中的图像作为blob图像保存到windows应用程序的图片框中。
这是我的密码。

private Image GetFaceFromDB()
    {

    Image fetchedImage;

        if (rowNumber >= 0)
        {
            byte[] faceTobyte = (byte[])dataTable.Rows[rowNumber] 
            ["Image"];
            MemoryStream stream = new MemoryStream(faceTobyte);
            fetchedImage = Image.FromStream(stream);**//Error at this 
            line**
            return fetchedImage;
        }else
            {
                MessageBox.Show("There are no images in thedatabase,please 
                insert images.");
                return null;
            }
      }

这是按钮点击事件

private void btnFirst_Click(object sender, EventArgs e)
    {
    RefreshDBConnection();

    rowNumber = 0;
    pbTrainingSetImages.Image = GetFaceFromDB();
    tbUpdateFaceName.Text = dataTable.Rows[rowNumber]["Name"].ToString();
    lbFaceNumber.Text = (rowNumber + 1).ToString();
    if(pbTrainingSetImages.Image != null)
    {
        MessageBox.Show("You have reached the First image in the 
    database!");
    }

    }

我还尝试了以下代码

private void btnFirst_Click(object sender, EventArgs e)
   {

        RefreshDBConnection();
        rowNumber = 0;
        pbTrainingSetImages.Image = GetFaceFromDB();
        tbUpdateFaceName.Text = dataTable.Rows[rowNumber] 
 ["Name"].ToString();
        lbFaceNumber.Text = (rowNumber + 1).ToString();
        if(pbTrainingSetImages.Image != null)
        {
            MessageBox.Show("You have reached the First image in the 
        database!");
        }

   }

b

private Image GetFaceFromDB()
        {

            Image fetchedImage;

            if (rowNumber >= 0)
            {
            byte[] faceTobyte= (byte[])dataTable.Rows[rowNumber["Image"];
                MemoryStream stream = new MemoryStream();
                stream.Position = 0;
                return fetchedImage = Image.FromStream(stream, false);
            }
            else
            {
                MessageBox.Show("There are no images in the database, please 
     insert 
                images.");
                return null;
            }
        }

c

private Image GetFaceFromDB()
        {

        Image fetchedImage;

        if (rowNumber >= 0)
        {
          byte[] faceTobyte = (byte[])dataTable.Rows[rowNumber]["Image"];
            MemoryStream stream = new MemoryStream();
            stream.Position = 0;
            return fetchedImage = Image.FromStream(stream, false);
        }
        else
        {
        MessageBox.Show("There are no images in the database,please insert 
            images.");
            return null;
        }
       }

d

private Image GetFaceFromDB()
       {

            Image fetchedImage;

            if (rowNumber >= 0)
            {
                byte[] faceTobyte = (byte[])dataTable.Rows[rowNumber] 
                ["Image"];
                using(var stream = new  MemoryStream(faceTobyte))
                {
                    fetchedImage = Image.FromStream(stream);
                    return fetchedImage;
                }

            }
            else
            {
              MessageBox.Show("There are no images in the database, please 
                insert images.");
                return null;
            }
        }

我还尝试了这些堆栈溢出链接中提供的大多数答案:
使用内存流将二进制文件转换为位图
image.memorystream:参数无效
参数无效
字节数组到图像的转换
这是错误消息参数在此行无效

fetchedImage = Image.FromStream(stream);


这是我的插入语句

private void AddFacesToDB(Image inputImage, string inputLabel)
    {
    if (connection.State.Equals(ConnectionState.Closed))
    {
        connection.Open();
    }

    try
    {
        //convert the face image to byte format for storing in db
        byte[] facesToByte = ConvertFaceToDBFormat(inputImage);
        totalRows++;

        //At what location is the image being stored to
        MessageBox.Show($"Adding row {totalRows.ToString()}");

        //inserting the image into the row 
        MySqlCommand insertCommand = new MySqlCommand("insert into 
     testimages.image_information(FaceID, FaceNo, Name, Image) values('" + 
       totalRows.ToString() + "','" + totalRows.ToString() + "', '" + 
       tbSetFaceLabel.Text + "', @IMG)", connection);

        MySqlParameter imageParam = 
       insertCommand.Parameters.AddWithValue("@IMG","Image");
        imageParam.Value = facesToByte;
        imageParam.Size = facesToByte.Length;
        int rowAffected = insertCommand.ExecuteNonQuery();
        MessageBox.Show("Data Stored Successfully in " + 
        rowAffected.ToString() + " Rows");

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

    }

这是我的数据库

o4tp2gmn

o4tp2gmn1#

我终于解决了。在花了6个小时之后。坚持是有好处的。我的陈述是问题出在我身上,

byte[] faceTobyte = (byte[])dataTable.Rows[rowNumber]["Image"];

我一直在把“图像”解析成字节,不知道我没有解析一个有效的图像格式。所以我把我的插入语句编辑成:

//At what location is the image being stored to
            MessageBox.Show($"Adding row {totalRows.ToString()}");

            //inserting the image into the row 
        MySqlCommand insertCommand = new MySqlCommand("insert into testimages.image_information(FaceID, FaceNo, Name, Image) 
            values(@faceid, @faceno, @name, @img)", connection);

            MySqlParameter imageParam;
            insertCommand.Parameters.Add("@faceid", MySqlDbType.Int16,25);
           insertCommand.Parameters.Add("@faceno", MySqlDbType.Int16, 25);
          insertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 25);
            insertCommand.Parameters.Add("@img", MySqlDbType.Binary);

        insertCommand.Parameters["@faceid"].Value = totalRows.ToString();
        insertCommand.Parameters["@faceno"].Value = totalRows.ToString();
            insertCommand.Parameters["@name"].Value = tbSetFaceLabel.Text;
            insertCommand.Parameters["@img"].Value = facesToByte;
            int rowAffected = insertCommand.ExecuteNonQuery();
            MessageBox.Show("Data Stored Successfully in " 
            + rowAffected.ToString() + " Rows");

现在,正在传递正确的图像格式,并且图像显示在图片框中。

相关问题