0

我的 Windows 窗体中有一个PictureBox控件。
图片列的数据类型是表 'TableName' 中的 'image'
这些这里的代码说,从数据库中获取图像并将其置于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();

通常它总是有效,但现在它ArgumentExeption在这一行显示错误“参数无效”pictureBox1.Image = Image.FromStream(stream);
我不明白?哪个参数?

任何帮助将不胜感激。

4

1 回答 1

1

它似乎位于您从数据库中保存和读取对象的方式上,例外来自 Image.FromStream(stream); 方法,MSDN说这个异常:

流没有有效的图像格式 - 或 - 流为空。

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

于 2012-11-30T06:55:50.557 回答