System.ArgumentException:参数无效.在C#中

本文关键字:无效 参数 ArgumentException System | 更新日期: 2023-09-27 18:00:12

嗨,我正在SQL server中存储图像。数据类型为Image。

当从数据库中检索图像时,我得到了类似的错误

System.ArgumentException:参数无效。

这一行出现的错误img = Image.FromStream(ms)

private void RetriveImage_Click(object sender, EventArgs e)    
{    
    SqlConnection con = new SqlConnection(constr);        
    con.Open();   
    Image img;
    try
    {
        cmd = new SqlCommand("select Pic from emguimg where ID =" + cbxID.SelectedItem, con);
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        byte[] bytes = (byte[])dt.Rows[0]["Pic"];
        MemoryStream ms = new MemoryStream(bytes);
        ms.Write(bytes, 0, bytes.Length);
        img = Image.FromStream(ms);

        PicBox.Image = img;
        PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
        PicBox.BorderStyle = BorderStyle.Fixed3D;
        ms.Flush();
        ms.Close();
    }
    catch (Exception ex)
    {
        WriteLogMessage(ex.ToString());
    }
} 

System.ArgumentException:参数无效.在C#中

填充MemoryStream后,在从此流加载图像之前,查找0的位置。

ms.Write(bytes, 0, bytes.Length);
ms.Position = 0; //insert this line
img = Image.FromStream(ms);