C# 使用内存流从数据库检索图像时出现无效参数错误

C# 使用内存流从数据库检索图像时出现无效参数错误,c#,database,C#,Database,我正在使用VS2012在C#中开发一个窗口表单应用程序。我在以下代码中遇到错误: if (dt.Rows[0]["Photo"] != System.DBNull.Value) //Retrieve image into PictureBox from database { photo_aray = (byte[])dt.Rows[0]["Photo"]; //where photo_aray is byte[]

我正在使用VS2012在C#中开发一个窗口表单应用程序。我在以下代码中遇到错误:

if (dt.Rows[0]["Photo"] != System.DBNull.Value)                     //Retrieve image into PictureBox from database
{
       photo_aray = (byte[])dt.Rows[0]["Photo"];            //where    photo_aray is byte[] photo_aray;
       MemoryStream ms = new MemoryStream(photo_aray);
       ms.Seek(0, SeekOrigin.Begin);
       Pic.Image = Image.FromStream(ms);                //Where Pic is the pictureBox(Error is getting in this line of "Invalid Parameter")
}
我使用下面的代码将图像转换为字节码并保存到
image
datatype数据库的“Photo”列中:

if (Pic.Image != null)
{
       ms = new MemoryStream();
       Pic.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
       byte[] photo_aray = new byte[ms.Length];
       ms.Position = 0;
       ms.Read(photo_aray, 0, photo_aray.Length);
}

sqlcommand cmd = new sqlcommand("INSERT into tbData (Photo) VALUES (photo_aray)",con);
cmd.ExecuteNonQuery();

如果有人知道,请帮忙。谢谢

请注意,您没有正确关闭MemoryStream。如果您使用“using”语句会更好。@Kolky您能告诉我应该使用哪个“using”语句吗。我正在使用“using System.IO;”语句已经存在。请在此处阅读更多信息:.NET中的所有流都继承自
IDisposable
,因此需要一个using语句,因为它将关闭它可能持有的任何未完成的IO锁。@Kolky谢谢。请使用“using语句”进行图像检索,以共享代码。请