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

时间:2016-03-28 20:59:30

标签: c# database

我正在使用VS2012在C#中使用Window Form应用程序。我在下面的代码中收到错误:

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")
}

我使用下面的代码将图像转换为字节代码并保存到" Photo" image数据类型的数据库列:

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

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

0 个答案:

没有答案