System.ArgumentException: Parameter is not valid.

Jun 29 2015 3:42 AM
Hi am storing images in sql server. The datatype is Image.

while retrieving the Images from the database i getting error like System.ArgumentException: Parameter is not valid.

The error coming in this line "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.ToString() + "", con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
byte[] bytes = (byte[])((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());
}
}

Answers (4)