我写了以下代码,用于在C#中从数据库传递图片到图片框。我从微软公司获取了这段代码。这是该页面的URL链接:Microsoft。
当我运行此代码时,它显示参数无效异常。
这段代码有什么问题?
我收到了以下错误信息:
未处理的异常类型 'System.ArgumentException' 发生在 System.Drawing.dll 中
附加信息: 参数无效。
这是我的数据库快照。 登录表
当我运行此代码时,它显示参数无效异常。
这段代码有什么问题?
private void button2_Click(object sender, EventArgs e)
{
try
{
String strCn =@"Data Source=DESKTOP-ROF2H0M\BHAGI;Initial Catalog=Golden;Integrated Security=True";
SqlConnection cn = new SqlConnection(strCn);
cn.Open();
//Retrieve BLOB from database into DataSet.
SqlCommand cmd = new SqlCommand("SELECT User_id ,img FROM login", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "login");
int c = ds.Tables["login"].Rows.Count;
if (c > 0)
{ //BLOB is read into Byte array, then used to construct MemoryStream,
//then passed to PictureBox.
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["login"].Rows[c-1]["img"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image = Image.FromStream(stmBLOBData);
}
cn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我收到了以下错误信息:
未处理的异常类型 'System.ArgumentException' 发生在 System.Drawing.dll 中
附加信息: 参数无效。
这是我的数据库快照。 登录表
PictureBox
提供的图像无效时会显示此错误消息。首先将您的字节数组保存到文件中(作为调试步骤)以确认图像是否有效;它可能是无效的。 - Martin