您好我在SQL Server中存储图像。数据类型为Image。
从数据库中检索图像时出现错误,如
System.ArgumentException:参数无效。
此行img = Image.FromStream(ms)
中出现错误。
private void RetriveImage_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constr);
con.Open();
Image img;
try
{
cmd = new SqlCommand("select Pic from emguimg where ID =" + cbxID.SelectedItem, con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
byte[] bytes = (byte[])dt.Rows[0]["Pic"];
MemoryStream ms = new MemoryStream(bytes);
ms.Write(bytes, 0, bytes.Length);
img = Image.FromStream(ms);
PicBox.Image = img;
PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
PicBox.BorderStyle = BorderStyle.Fixed3D;
ms.Flush();
ms.Close();
}
catch (Exception ex)
{
WriteLogMessage(ex.ToString());
}
}
答案 0 :(得分:0)
在从此流加载图片之前填写MemoryStream
寻找位置0
之后。
ms.Write(bytes, 0, bytes.Length);
ms.Position = 0; //insert this line
img = Image.FromStream(ms);