繁体   English   中英

无法将图像从数据库检索到图片框

[英]Unable to retrieve image from database to picturebox

我的Windows窗体中有一个PictureBox控件。
表“ TableName”中“图片”列的数据类型为“图像”
这些是下面的代码,它表示从数据库中获取图像并将其放入PictureBox控件中:

string connectionString = @"Initial Catalog=DataBaseName;Data Source=DataSourceName;Integrated Security=SSPI;";
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            SqlDataAdapter da = new SqlDataAdapter(new SqlCommand("Select Picture From TableName where ID = 2 ", connection));
            DataSet ds = new DataSet();
            da.Fill(ds);
            byte[] myImage = new byte[0];
            myImage = (byte[])ds.Tables[0].Rows[0]["Picture"];
            MemoryStream stream = new MemoryStream(myImage);
            pictureBox1.Image = Image.FromStream(stream);
            connection.Close();

通常它总是可以工作,但是现在在此行显示错误为“ Paramerter无效”的ArgumentExeption pictureBox1.Image = Image.FromStream(stream);
我听不懂 哪个参数?

任何帮助将不胜感激。

似乎是从数据库保存和读取对象的方式上出现的,异常来自Image.FromStream(stream); MEthode, MSDN谈到了此异常:

该流没有有效的图像格式-或者-stream为null。

因此,正如您在问题中提到的不是Null一样,我假设您保存数据或以不兼容的方式读取数据。

暂无
暂无

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

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