繁体   English   中英

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

[英]System.ArgumentException: Parameter is not valid. in C#

嗨,我将图像存储在SQL Server中。 数据类型是图像。

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

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());
    }
} 

填充MemoryStream之后,在从该流加载图像之前,先将位置定位为0

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM